简体   繁体   中英

NASM division and multiplication

I am trying to implement a formula for trapezoidal area using assembly, but came across with passing "ax" register to result variable. Where I have done mistake? Can someone point me please. Or is a problem with calculations? As I understood after division I can pass to bl different value, when "ax" will stay with a result of "5" and then I can do multiplication and pass ax to the result which will be "15"

section .data
a : dw 3
b : dw 7
h : dw 3
n : dw 2
result: dw 0

section .text
global main

main:
mov ax,[a]
add ax,[b]
mov bl,[n]
div bl
mov bl,[h]
mul bl
mov result,ax
mov eax,1
mov ebx,0
int 80h

To compile and run it I am using following script:

nasm -f elf -l $1.lst $1.asm
gcc -m32 -o $1 $1.o

As I understood after division I can pass to bl different value, when "ax" will stay with a result of "5"

This is not entirely true! In your example AX was equal to 5 because the previous division resulted in a remainder of zero. So all you can say is: when "al" will stay with a result of "5" .

 a : dw 3 b : dw 7 h : dw 3 n : dw 2 

Please do attach the colons to the label names. Alternatively don't write them at all (Data definitions don't need them).

 mov bl,[n] div bl mov bl,[h] 

Since NASM doesn't care about sizes too much, you get away with assigning these word sized variables to byte sized registers. At times this is useful but you need to keep aware of it!

 mov result,ax 

This of course is the true problem. To have NASM write AX in the result variable, you have to use the brackets [] .

mov [result], ax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM