Question: I need the following assembly x86 code to work plz help. The code can't use any library. The code consist of taking 2 user inputs
I need the following assembly x86 code to work plz help. The code can't use any library. The code consist of taking 2 user inputs and giving the adding, minus, mult and div results.
.MODEL SMALL .STACK .data NEWLINE db 0Dh, 0Ah, '$' msg1 db "Enter first number: ",0 msg2 db "Enter second number: ",0 num1 db ? num2 db ? Addition db ? Subtraction db ? Multiply db ? Division db ? CHARACTER db ? STRING db 81 dup('$') MAX_LEN equ 80 CARRIAGE_RETURN equ 0Dh LINE_FEED equ 0Ah IS_VALID db ? UNSIGNED_INT dw ? SIGNO db ? TRUE equ 1h FALSE equ 0h Result_Addition db "The sum of num1 & num2 is: ",0 Result_Subtraction db "The result of subtraction is: ",0 Result_Multiplication db "The result of multiplication is: ",0 Result_Division db "The result of division is: ",0
.code main PROC mov dx,OFFSET msg1 call IS_UNSIGNED_INT call STRING_TO_UNSIGNED_INT mov num1,ax mov dx,OFFSET msg2 call IS_UNSIGNED_INT call STRING_TO_UNSIGNED_INT
mov num2,ax mov ax,num1 add ax,num2 mov addition,ax mov dx,OFFSET Result_Addition call STRING_TO_UNSIGNED_INT call UNSIGNED_INT_TO_STRING call NEW_LINE
mov ax,num1 sub ax,num2 mov subtraction,ax mov dx,OFFSET Result_Subtraction call STRING_TO_UNSIGNED_INT call UNSIGNED_INT_TO_STRING call NEW_LINE
mov ax,num1 mul num2 mov multiply,ax mov dx,OFFSET Result_Multiplication call STRING_TO_UNSIGNED_INT call UNSIGNED_INT_TO_STRING call NEW_LINE
mov ax,num1 sub dx,dx div num2 mov division,ax mov dx,OFFSET Result_Division call STRING_TO_UNSIGNED_INT call UNSIGNED_INT_TO_STRING call NEW_LINE
main ENDP
;***********************************************************************************
UNSIGNED_INT_TO_STRING PROC
mov ax, UNSIGNED_INT
mov bx, 10
mov di, 0h
cmp byte ptr SIGNO,-1
jnz ANOTHER
imul SIGNO
ANOTHER:
mov dx, 0h
div bx
add dl, '0'
mov STRING[di], dl
inc di
cmp ax, 0h
jnz ANOTHER
cmp SIGNO, -1
jnz NO_NEGATIVE
mov byte ptr STRING[di], '-'
inc di
NO_NEGATIVE:
mov STRING[di], '$'
dec di
mov si, 0
OTHER:
mov ch, STRING[di]
mov cl, STRING[si]
mov STRING[di], cl
mov STRING[si], ch
dec di
inc si
cmp si, di
jb OTHER
RET
UNSIGNED_INT_TO_STRING ENDP
;***********************************************************************************
STRING_TO_UNSIGNED_INT PROC
mov ax, 0h
mov cx, 0h
mov bx, 10
mov di, 0h
mov cl, STRING[di]
cmp cl, '-'
jz GOTO
cmp cl, '+'
jnz BACK
GOTO:inc di
mov cl, STRING[di]
BACK:
cmp cl, '$'
jz CONVERTED
mul bx
sub cl, '0'
add ax, cx
inc di
mov cl, STRING[di]
jmp BACK
CONVERTED:
imul SIGNO
mov UNSIGNED_INT, ax
RET
STRING_TO_UNSIGNED_INT ENDP
;***********************************************************************************
IS_UNSIGNED_INT PROC
mov byte ptr IS_VALID, TRUE
mov di, 0h
mov ah, STRING[di]
mov byte ptr SIGNO,+1
cmp ah, '-'
jnz ISPOSITIVE
mov byte ptr SIGNO,-1
jmp PROXIMO_CHAR
ISPOSITIVE:cmp ah, '+'
jnz NEXT_CHAR
PROXIMO_CHAR:inc di
mov ah, STRING[di]
NEXT_CHAR:
cmp ah, '$'
jz DECIDIR
cmp ah, '0'
jb DECIDIR
cmp ah, '9'
ja DECIDIR
inc di
mov ah, STRING[di]
jmp NEXT_CHAR
DECIDIR:
jnz NOT_VALID
cmp di, 0h
jnz KEEP
NOT_VALID:
mov byte ptr IS_VALID, FALSE
KEEP:
RET
IS_UNSIGNED_INT ENDP
;***********************************************************************************
;***********************************************************************************
SHOW_CHARACTER PROC
mov ah, 02h ; write a character to standard ouput mov dl, CHARACTER ; DL has the character int 21H RET
SHOW_CHARACTER ENDP
;***********************************************************************************
ASK_CHARACTER PROC
mov ah, 08h ; read a character without echo from standard input int 21h ; AL stores the character read mov CHARACTER, al RET
ASK_CHARACTER ENDP
;************************************************************************
NEW_LINE PROC
mov dx, offset NEWLINE ; write all characters mov ah, 9 ; in the standard output int 21h ; DX has the offset of the first character RET ; the last character must be '$'
NEW_LINE ENDP
;***********************************************************************************
ASK_STRING PROC
mov di, 0h
GET_NEXT: call ASK_CHARACTER call SHOW_CHARACTER mov bl, CHARACTER cmp bl, CARRIAGE_RETURN jz EOL cmp di, MAX_LEN je SKIP mov STRING[di], bl inc di SKIP: jmp GET_NEXT EOL: mov STRING[di], '$' call CLEAR_KEYBOARD_BUFFER RET ASK_STRING ENDP
;***********************************************************************************
SHOW_STRING PROC
mov dx, offset STRING ; write all characters mov ah, 09h ; in the standard output int 21h ; DX has the offset of the first character RET ; the last character must be '$'
SHOW_STRING ENDP
;***********************************************************************************
CLEAR_KEYBOARD_BUFFER PROC
mov ah, 0bh ; check standard input status int 21h ; AL = 0 if input buffer is empty cmp al, 0h ; jz CLEARED ; are there no more characters in the standard input
NEXT:
call ASK_CHARACTER mov ah, 0bh ; check standard input status int 21h ; AL = 0 if input buffer is empty cmp al, 0h ; jnz NEXT ; are there more characters in the standard input
CLEARED: RET CLEAR_KEYBOARD_BUFFER ENDP
end main
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
