Program solving equation in Assembly -
i have problem simple program in assembly. i'm using dosbox , tasm . have problem program. operand types not match @ line 76 78 80 . after multiplication. tried make changes using difftrent variable size
; -------------------------------------------- ; equation=(a+c*b)/d-2*c, ; -------------------------------------------- .model small .stack 100h .data db 0 b db 0 c db 0 d db 0 result1 db ? result2 db ? message1 db "equation: (a+c*b)/d-2*c a=$" message2 db "b=$" message3 db "c=$" message4 db "d=$" message5 db "result=$" .code start: mov ax,@data mov ds,ax mov ax, seg message1 ;get , save variable mov ds,ax mov dx,offset message1 mov ah, 9h int 21h mov ah, 1h int 21h sub al,30h ;converting real number mov a,al mov ax, seg message2 ;get b , save variable mov ds,ax mov dx,offset message2 mov ah, 9h int 21h mov ah, 1h int 21h sub al,30h ;converting real number mov b,al mov ax, seg message3 ;get c , save variable mov ds,ax mov dx,offset message3 mov ah, 9h int 21h mov ah, 1h int 21h sub al,30h ;converting real number mov c,al mov ax, seg message4 ;get d , save variable mov ds,ax mov dx,offset message4 mov ah, 9h int 21h mov ah, 1h int 21h sub al,30h ;converting real number mov d,al mov al,b ; (a+c*b) ------------------------error mul c add ax,a ; ------------------------error push ax ;save current ax mov ax,c ;d-2*c------------------------error shl ax,2 sub d,ax pop bx ;get previous ax bx div bx ; div ax:bx mov result1,al mov result2,ah add result1,30h ;converting string add result2,30h ;converting string mov al,result1 mov bl,result2 mov ax, seg message5 mov ds,ax mov dx,offset message5 mov ah, 9h int 21h mov al,result1 mov bl,result2 mov dl, al mov ah , 2h int 21h mov dl, bl mov ah , 2h int 21h mov ax,4c00h int 21h end start
your program good, have issues operand sizes, normal. took code , made little changes, changes commented , pointed arrows (<========) , :
- fixed operand size problem. still use db because noticed capturing numbers single chars.
- the result of (d-2*c) stored in bx. because need divide (a+c*b) / (d-2*c), , popping (a+c*b) in bx, so, when
div bx
doing (d-2*c) / (a+c*b) . - separated display quotient , remainder.
- added
13,10
line breaks messages. - fixed
shl ax,2
shl ax,1
. 1shl
x2, 2shl
x2x2. - the remainder obtained
dl
because whendiv
uses word divisor, remainder left indx
.
here code little changes (tested on emu8086):
; -------------------------------------------- ; equation=(a+c*b)/d-2*c, ; --------------------------------------------.model small .stack 100h .data db 0 b db 0 c db 0 d db 0 result1 db ? result2 db ? message1 db 13,10,"equation: (a+c*b)/d-2*c",13,10,"a=$" message2 db 13,10,"b=$" ;<================= 13,10 message3 db 13,10,"c=$" ;<================= linebreak. message4 db 13,10,"d=$" ;<================= message5 db 13,10,"quotient=$" ;<================= message6 db 13,10,"remainder=$" ;<================= .code start: mov ax,@data mov ds,ax mov ax, seg message1 ;get , save variable mov ds,ax mov dx,offset message1 mov ah, 9h int 21h mov ah, 1h int 21h sub al,30h ;converting real number mov a,al mov ax, seg message2 ;get b , save variable mov ds,ax mov dx,offset message2 mov ah, 9h int 21h mov ah, 1h int 21h sub al,30h ;converting real number mov b,al mov ax, seg message3 ;get c , save variable mov ds,ax mov dx,offset message3 mov ah, 9h int 21h mov ah, 1h int 21h sub al,30h ;converting real number mov c,al mov ax, seg message4 ;get d , save variable mov ds,ax mov dx,offset message4 mov ah, 9h int 21h mov ah, 1h int 21h sub al,30h ;converting real number mov d,al mov al,b ; (a+c*b) mul c mov cl,a ;<======== mov cx mov ch,0 ;<======== add ax. add ax,cx ;<======== c*b + a. ;push ax ;<======== no longer necessary because ;<======== in next block use bx. mov bl,c ;<======== mov c bl , clear mov bh,0 ;<======== bh. c in bx. shl bx,1 ;<======== 2*c. 1 shift x2, 2 shifts x2x2. sub d,bl ;d - 2c mov bl,d ;<======== mov d bl , clear bh. mov bh,0 ;<======== d in bx. bx = (d-2c). ;pop ax ;<======== no longer necessary. ax contains (a+c*b). mov dx,0 ;<======== clear dx, because divisor word. ;<======== when divisor word, div uses dx:ax. div bx ;<======== dx:ax / bx == dx:(a+c*b) / (d-2c). ;<======== division unsigned, signed use idiv. mov result1,al ;<===== quotient. mov result2,dl ;<===== remainder, because divisor word. add result1,30h ;converting string add result2,30h ;converting string mov al,result1 mov bl,result2 ;display quotient <============= mov ax, seg message5 mov ds,ax mov dx,offset message5 mov ah, 9h int 21h mov al,result1 mov dl, al mov ah , 2h int 21h ;display remainder <============= mov ax, seg message6 mov ds,ax mov dx,offset message6 mov ah, 9h int 21h mov dl, bl mov ah , 2h int 21h mov ax,4c00h int 21h end start
next "to do" list:
- change size of operands db dw, allow program handle bigger numbers.
- change div idiv, because div unsigned while idiv signed. idiv let handle negative results.
- capture numbers int=21h ah=0ah strings (not single chars). later, convert strings numbers. next 2 links take procedures convert string number :
assembly x86 date number - breaking string smaller sections
32 bit calculator in 8086 assembly
finally, test data :
(a+c*b) / (d-2*c) a=1 b=2 c=3 d=8 a+c*b = 1+3*2 = 7 d-2*c = 8-2*3 = 2 7 / 2 = quotient 3, remainder 1
Comments
Post a Comment