x86 assembly programming - Calculator Program The cheap calculator For this proj
ID: 3697683 • Letter: X
Question
x86 assembly programming - Calculator Program
The cheap calculator For this project, we will build a simple integer 4 function calculator. The functions will be addition, subtraction, multiplication and division. The project will be simplified by limiting the inputs to unsigned values less than or equal to 255. It will be possible to obtain sums greater than what fits into 8 bits, so that will have to be accounted for. Also, it will be possible to obtain differences that go negative. This will be accounted for. As for multiplication, we will see products that go into 16 bits. Division could yield a quotient with remainder in the 8 bit range In order to keep this project as simple as possible, we will make the following conditions: 1. Division will be limited to word divided by word operands. 2. Division will yield a quotient and remainder. Both must be displayed. No floating numbers 3. Multiplication will display up to 16 bit results, so you will need a 16 bit binary to ASCII conversion subroutine (Sound familiar?) 4. For negative values from subtraction, we will two's complement the result and print it with a minus sign in front of it. This will avoid the need for a special negative number conversion subroutine 5. For 9 bit sums, the low order 8 bits will go into AL, and the carry will be added into AH The standard 16 bit binary to ASCII conversion subroutine may be used on AX How to do this: Start with the dreaded flow chart. Write the program to read in values and then parse them Parse means to break something complex into simpler pieces. For example 147 202 There are 4 elements in this input, the first value, 147, the augend; an operation, plus; the second number, 202, the addend; and the final control character that will start the computation, the equal sign. We can use the spaces in the input as separators to help us break the input apart 1.You can use INT 21H, function 0AH to read in a string from the console. Point an index register, Sl or DI, to the start of the data. Remember, that start of data will be located at input + 2 2. Start reading bytes, one at a time from the input, transferring the bytes into a temporary memory buffer. When you hit the space, the first number has been read in. You may wantExplanation / Answer
section .data
; Messages
val1 db 10,'-Calculator-',10,0
lval1 equ $ - val1
val2 db 10,'Number 1: ',0
lval2 equ $ - val2
val3 db 'Number 2: ',0
lval3 equ $ - val3
val4 db 10,'1. Add',10,0
lval4 equ $ - val4
val5 db '2. Subtract',10,0
lval5 equ $ - val5
val6 db '3. Multiply',10,0
lval6 equ $ - val6
val7 db '4. Divide',10,0
lval7 equ $ - val7
val8 db 'Operation: ',0
lval8 equ $ - val8
val9 db 10,'Result: ',0
lval9 equ $ - val9
val10 db 10,'Invalid Option',10,0
lval10 equ $ - val10
nlinea db 10,10,0
lnlinea equ $ - nlinea
section .bss
; Spaces reserved for storing the values ??provided by the user.
opc: resb 2
num1: resb 2
num2: resb 2
result: resb 2
section .text
global _start
_start:
; Print on screen the message 1
mov eax, 4
mov ebx, 1
mov ecx, val1
mov edx, lval1
int 80h
; Print on screen the message 2
mov eax, 4
mov ebx, 1
mov ecx, val2
mov edx, lval2
int 80h
; We get num1 value.
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 2
int 80h
; Print on screen the message 3
mov eax, 4
mov ebx, 1
mov ecx, val3
mov edx, lval3
int 80h
; We get num2 value.
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 2
int 80h
; Print on screen the message 4
mov eax, 4
mov ebx, 1
mov ecx, val4
mov edx, lval4
int 80h
; Print on screen the message 5
mov eax, 4
mov ebx, 1
mov ecx, val5
mov edx, lval5
int 80h
; Print on screen the message 6
mov eax, 4
mov ebx, 1
mov ecx, val6
mov edx, lval6
int 80h
; Print on screen the message 7
mov eax, 4
mov ebx, 1
mov ecx, val7
mov edx, lval7
int 80h
; Print on screen the message 8
mov eax, 4
mov ebx, 1
mov ecx, val8
mov edx, lval8
int 80h
; We get the option selected.
mov ebx,0
mov ecx,opc
mov edx,2
mov eax,3
int 80h
mov ah, [opc] ; Move the selected option to the registry ah
sub ah, '0' ; Convert from ascii to decimal
; We compare the value entered by the user to know what operation to perform.
cmp ah, 1
je add
cmp ah, 2
je subtract
cmp ah, 3
je multiply
cmp ah, 4
je divide
; If the value entered by the user does not meet any of the above
; conditions then we show an error message and we close the program.
mov eax, 4
mov ebx, 1
mov ecx, val10
mov edx, lval10
int 80h
jmp exit
add:
; We keep the numbers in the registers al and bl
mov al, [num1]
mov bl, [num2]
; Convert from ascii to decimal
sub al, '0'
sub bl, '0'
; Add
add al, bl
; Conversion from decimal to ascii
add al, '0'
; We move the result
mov [result], al
; Print on screen the message 9
mov eax, 4
mov ebx, 1
mov ecx, val9
mov edx, lval9
int 80h
; Print on screen the result
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, 2
int 80h
; We end the program
jmp exit
subtract:
; We keep the numbers in the registers al and bl
mov al, [num1]
mov bl, [num2]
; Convert from ascii to decimal
sub al, '0'
sub bl, '0'
; Subtract
sub al, bl
; Conversion from decimal to ascii
add al, '0'
; We move the result
mov [result], al
; Print on screen the message 9
mov eax, 4
mov ebx, 1
mov ecx, val9
mov edx, lval9
int 80h
; Print on screen the result
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, 1
int 80h
; We end the program
jmp exit
multiply:
; We store the numbers in registers al and bl
mov al, [num1]
mov bl, [num2]
; Convert from ascii to decimal
sub al, '0'
sub bl, '0'
; Multiply. AX = AL x BL
mul bl
; Conversion from decimal to ascii
add ax, '0'
; We move the result
mov [result], ax
; Print on screen the message 9
mov eax, 4
mov ebx, 1
mov ecx, val9
mov edx, lval9
int 80h
; Print on screen the result
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, 1
int 80h
; We end the program
jmp exit
divide:
; We store the numbers in registers ax and bx
mov al, [num1]
mov bl, [num2]
mov dx, 0
mov ah, 0
; Convert from ascii to decimall
sub al, '0'
sub bl, '0'
; Division. AL = AX / BX
div bl
; Conversion from decimal to ascii
add ax, '0'
; We move the result
mov [result], ax
; Print on screen the message 9
mov eax, 4
mov ebx, 1
mov ecx, val9
mov edx, lval9
int 80h
; Print on screen the result
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, 1
int 80h
; We end the program
jmp exit
exit:
; Print on screen two new lines
mov eax, 4
mov ebx, 1
mov ecx, nlinea
mov edx, lnlinea
int 80h
; End the program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.