-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitb-calculator.asm
212 lines (167 loc) · 3.55 KB
/
itb-calculator.asm
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
; catana ioan-alexandru
; bti final grade & credit points calculator
; 13/01/2023
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
section .data
; data declaration
; input messages
msg0: db "Please be realistic! Input single-digit numbers only!", 0xA
msg0len: equ $ - msg0
msg1: db "Test #1 grade: "
msg1len: equ $ - msg1
msg2: db "Test #2 grade: "
msg2len: equ $ - msg2
msg3: db "Exam grade: "
msg3len: equ $ - msg3
msg4: db 0xA, "Final grade: "
msg4len: equ $ - msg4
msg5: db 0xA, "Credit points obtained: "
msg5len: equ $ - msg5
msg6: db 0xA, 0xD
msg6len: equ $ - msg6
; sum
sum: dw 0
; useful rounding variable
fifty: db 50
section .bss
; memory reservations
res: resb 1
; grades obtained
test1: resb 2
test2: resb 2
exam: resb 2
; useful credit points variable
twodigits: resb 2
section .text
global _start
_start:
; code starts here
; display important message
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg0
mov edx, msg0len
int 0x80
; read the obtained grades
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, msg1len
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, test1
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, msg2len
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, test2
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, msg3len
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, exam
mov edx, 2
int 0x80
; convert from ascii to decimal number
mov al, [test1]
sub al, '0'
mov [test1], al
mov al, [test2]
sub al, '0'
mov [test2], al
mov al, [exam]
sub al, '0'
mov [exam], al
; computing the weighted sum
mov al, [test1]
mov bl, 15
mul bl ; result in AH AL
mov bx, [sum]
add bx, ax
mov [sum], bx
mov al, [test2]
mov bl, 15
mul bl
mov bx, [sum]
add bx, ax
mov [sum], bx
mov al, [exam]
mov bl, 70
mul bl
mov bx, [sum]
add bx, ax
mov [sum], bx
; computing the final grade
mov ax, [sum]
mov bl, 100
div bl ; al is the result, ah contains the remainder
add al, '0'
mov [res], al
; round the grade
mov bh, [fifty]
cmp ah, bh
jge roundup ; if the remainder is >=50, the grade is rounded up
jmp results
roundup:
mov al, [res]
add al, 1
mov [res], al
jmp results
results:
; display results
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg4
mov edx, msg4len
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, res
mov edx, 1
int 0x80
; compute credit
mov al, [res]
sub al, '0'
mov [res], al
mov al, [res]
mov bl, 6
mul bl ; ax contains 16 bit result
aam ; ax contains unpacked BCD result
add ax, 3030h ; ah first digit, al last digit
mov [twodigits], ah
mov [twodigits + 1], al
; displaying the credit points message
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg5
mov edx, msg5len
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, twodigits
mov edx, 2
int 0x80
; prints new line
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg6
mov edx, msg6len
int 0x80
; end of program
mov eax, SYS_EXIT
int 0x80