calculator - Assembly multiplication and addition -
i can't seem output integer in assembly greater 128.. works fine until add/multiply numbers resulting integer > 128. returns garbage character. help! how can output integers. here complete code.
title .model small .stack 64 .data msga db 13,10,"input equation: ","$" msgb db 13,10,"the sum ","$" msgc db 13,10,"the difference ","$" msgd db 13,10,"the product ","$" msge db 13,10,"the quotient ","$" msgf db 13,10,"the difference -","$" msgg db 13,10,"the remainder ","$" op1a db ? op1b db ? oprnd db ? op2a db ? op2b db ? result db ? num1 db ? num2 db ? ;for 3 digit outputs first db ? second db ? third db ? .code main proc near mov ax, @data mov ds, ax @strt: lea dx, msga mov ah, 09h int 21h ;get tens digit of first number mov ah, 01h int 21h sub al, '0' mov op1a, al ;get ones digit of first number mov ah, 01h int 21h sub al, '0' mov op1b, al ;get operand mov ah, 01h int 21h mov oprnd, al ;get tens digit of second number mov ah, 01h int 21h sub al, '0' mov op2a, al ;get ones digit of second number mov ah, 01h int 21h sub al, '0' mov op2b, al xor ax, ax xor bx, bx ;form num2 mov al, op2a mov bl, 10 mul bl add al, op2b mov num2, al ;form num1 mov al, op1a mul bl add al, op1b mov num1, al xor ax, ax xor bx, bx ;check operand cmp oprnd, '+' je @add cmp oprnd, '*' je @multi cmp oprnd, '-' je @sub cmp oprnd, '/' je @div cmp oprnd, '%' je @div jmp @exit ;divide @div: cmp num2, 0 je @exit xor ax, ax mov al, num1 mov bl, num2 div bl cmp oprnd, '%' je @mod mov result, al lea dx, msge mov ah, 09h int 21h jmp @print ;modulo @mod: mov result, ah lea dx, msgg mov ah, 09h int 21h jmp @print ;subtract @sub: mov al, num2 cmp num1, al jge @pstv ;add negative sign lea dx, msgf mov ah, 09h int 21h sub al, num1 mov result, al jmp @print @pstv: mov al, num1 sub al, num2 mov result, al lea dx, msgc mov ah, 09h int 21h jmp @print ;multiply @multi: mov al, num2 mov bl, num1 mul bl mov result, al lea dx, msgd mov ah, 09h int 21h jmp @print ;add @add: mov al, num2 add al, num1 mov result, al lea dx, msgb mov ah, 09h int 21h jmp @print ;print result @print: cmp result, 9 jle @onedigit cmp result, 99 jle @twodigits ;separate first digit xor ax, ax xor bx, bx mov al, result mov bl, 100 div bl mov result, al mov result, ah ;output hundredths frigging digit add first, '0' mov dl, first mov ah, 02h int 21h @twodigits: xor ax, ax xor bx, bx ;separate 2 remaining frigging digits mov al, result mov bl, 10 div bl mov second, al mov third, ah ;output tens frigging digit add second, '0' mov dl, second mov ah, 02h int 21h ;output ones frigging digit add third, '0' mov dl, third mov ah, 02h int 21h jmp @exit @onedigit: add result, '0' ;print 1 digit mov dl, result mov ah, 02h int 21h @exit: mov ah, 4ch int 21h main endp ;-------- end main
you using 8-bit registers bl, al etc. range of 8-bit values +-128. use larger values need use 16 or 32-bit registers. names of 16-bit registers bx, ax etc. need define result dw (for 16-bit) instead of db.
see more info here:
Comments
Post a Comment