-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcelsius_fahrenheit_temperature_converter.asm
More file actions
131 lines (114 loc) · 4.35 KB
/
Copy pathcelsius_fahrenheit_temperature_converter.asm
File metadata and controls
131 lines (114 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
; =============================================================================
; TITLE: Temperature Conversion
; DESCRIPTION: Convert Celsius to Fahrenheit and vice versa. Demonstrates
; arithmetic operations with formulas.
; AUTHOR: Amey Thakur (https://github.com/Amey-Thakur)
; REPOSITORY: https://github.com/Amey-Thakur/8086-ASSEMBLY-LANGUAGE-PROGRAMS
; LICENSE: MIT License
; =============================================================================
.MODEL SMALL
.STACK 100H
;-----------------------------------------------------------------------------
; DATA SEGMENT
; Formulas:
; F = (C * 9/5) + 32
; C = (F - 32) * 5/9
;-----------------------------------------------------------------------------
.DATA
CELSIUS DW 25 ; Input temperature in Celsius
FAHRENHEIT DW ? ; Calculated Fahrenheit
MSG1 DB 'Celsius to Fahrenheit: $'
MSG2 DB 0DH, 0AH, 'Fahrenheit to Celsius: $'
NEWLINE DB 0DH, 0AH, '$'
;-----------------------------------------------------------------------------
; CODE SEGMENT
;-----------------------------------------------------------------------------
.CODE
MAIN PROC
; Initialize Data Segment
MOV AX, @DATA
MOV DS, AX
;=========================================================================
; CELSIUS TO FAHRENHEIT CONVERSION
; Formula: F = (C * 9 / 5) + 32
;=========================================================================
LEA DX, MSG1
MOV AH, 09H
INT 21H
; Calculate: F = (C * 9 / 5) + 32
MOV AX, CELSIUS ; AX = 25 (Celsius)
MOV BX, 9
MUL BX ; AX = 25 * 9 = 225
MOV BX, 5
XOR DX, DX ; Clear DX for division
DIV BX ; AX = 225 / 5 = 45
ADD AX, 32 ; AX = 45 + 32 = 77 (Fahrenheit)
MOV FAHRENHEIT, AX ; Store result
; Display result (77F)
CALL PRINT_NUM
MOV DL, 'F'
MOV AH, 02H
INT 21H
;=========================================================================
; FAHRENHEIT TO CELSIUS CONVERSION
; Formula: C = (F - 32) * 5 / 9
;=========================================================================
LEA DX, MSG2
MOV AH, 09H
INT 21H
; Calculate: C = (F - 32) * 5 / 9
MOV AX, FAHRENHEIT ; AX = 77 (Fahrenheit)
SUB AX, 32 ; AX = 77 - 32 = 45
MOV BX, 5
MUL BX ; AX = 45 * 5 = 225
MOV BX, 9
XOR DX, DX ; Clear DX for division
DIV BX ; AX = 225 / 9 = 25 (Celsius)
; Display result (25C)
CALL PRINT_NUM
MOV DL, 'C'
MOV AH, 02H
INT 21H
;-------------------------------------------------------------------------
; Program Termination
;-------------------------------------------------------------------------
MOV AH, 4CH
INT 21H
MAIN ENDP
;-----------------------------------------------------------------------------
; PRINT_NUM: Print AX as decimal number
;-----------------------------------------------------------------------------
PRINT_NUM PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
XOR CX, CX ; Digit counter
MOV BX, 10 ; Divisor
DIV_LOOP:
XOR DX, DX ; Clear for division
DIV BX ; AX = quotient, DX = remainder
PUSH DX ; Save digit
INC CX ; Count digit
CMP AX, 0 ; More digits?
JNE DIV_LOOP
PRINT_LOOP:
POP DX ; Get digit
ADD DL, '0' ; Convert to ASCII
MOV AH, 02H ; DOS: Print character
INT 21H
LOOP PRINT_LOOP
POP DX
POP CX
POP BX
POP AX
RET
PRINT_NUM ENDP
END MAIN
;=============================================================================
; TEMPERATURE CONVERSION NOTES:
; - Celsius to Fahrenheit: F = C × 9/5 + 32
; - Fahrenheit to Celsius: C = (F − 32) × 5/9
; - Watch for integer division truncation
; - Water freezes: 0°C = 32°F, Water boils: 100°C = 212°F
;=============================================================================