In: Computer Science
;the task is to print out the result no matter how many digits ; it should work on Emu8086 ;the code should be in assembly language ; add comments to your code org 100h .data F db 0 num1 db 0 num2 db 0 op db 0 result db 0 msg db 0dh,0ah,"Error" .code mov ax,@data mov ds,ax LL1: call get1 cmp ax,0 je print cmp F,0 je LL1 LL2: call get2 cmp ax,0 je print cmp F,1 je LL2 Solve: cmp op,'+' je addition mov bl, num1 sub bl,num2 mov result,bl jmp printresult addition: mov bl, num1 add bl,num2 mov result,bl printresult: ;Task: add code to print result jmp done print: mov cx,7 mov si,0 screen: mov dl,msg[si] mov ah,2 int 21h inc si loop screen done: ret get1 proc mov ah,1 int 21h cmp al,'+' je L4 cmp al,'-' je L5 sub al,30h cmp al,0 jb L2 cmp al,9 ja L2 mov cx,10 mov bh,0 L3: add bh,num1 loop L3 add bh,al mov num1,bh ret L4: mov op,'+' mov F,1 ret L5: mov op,'-' mov F,1 ret L2: mov ax,0 ret endp get2 proc mov ah,1 int 21h cmp al,'=' je L41 sub al,30h cmp al,0 jb L21 cmp al,9 ja L21 mov cx,10 mov bh,0 L31: add bh,num2 loop L31 add bh,al mov num2,bh ret L41: mov F,2 ret L21: mov ax,0 ret endp
Greetings!!
Code:
.DATA
RES DB 4 DUP(?) ;array to store the decimal result
printresult:
LEA SI,RES ;load the address of result array
ADD SI,2 ;increments the array index by 2 to access the array from
last to first
PUSH AX ;saves the previous answers on to the stack
PUSH CX
PUSH DX
MOV CL,10 ;load 10 to CL for converting the answer to decimal
MOV AL,BL ;copy the answer into register AL
AGAIN:
MOV AH,0 ;clear AH
DIV CL ;divide the answer with 10 and store the remainder(is the
equivalent decinal) into the result array
ADD AH,30H ;add 30H to convert into ASCII
MOV [SI],AH ;store the decimal digit into the array
DEC SI ;decrement array index
CMP AL,0 ;check the convetion is over or not
JZ END ;if finished go to the label END
JMP AGAIN ;otherwise repeat the division process to convert the
answer to decimal
END:
LEA SI,RES
MOV [SI+3],'$' ;insert end of string character at the end of the
result array
MOV DX,OFFSET RES ;display the answer stored in the result
array
MOV AH,9
INT 21H
POP DX ;reload the registers
POP CX
POP AX
jmp done
Output screenshots:
Hope this helps