简体   繁体   中英

Assembly language, program to check if a number is divideable by 2 and print numbers in a loop

I've a macro that's working, and getting big numbers in assembly language. I've been trying to make a program that'd print numbers divideable by 2. It seems to me that something's wrong with the comparison script I've written.

mov cx, number1 
check:
mov dx, cx
mov bx, 2
div bx
cmp ax, 0
je divide
add ax, 1

loop:
divide:
printNumber ax
add ax, 2
cmp ax, number2
je end
jmp loop

If the number for which you want to test if it's divisible by 2 is in number1 and you know that the word division div bx actually divides DX:AX then you need to move the number to AX and zero DX .

check:
mov ax, number1 
xor dx, dx
mov bx, 2
div bx

The actual determination then comes from inspecting the remainder from this division by 2. The remainder is in the DX register!

cmp dx, 0
je  divisible

But all of this is overkill since testing to see if a number is even aka divisible by 2, is a simple matter of looking at its lowest bit:

test number1, 1
jz   divisible

Hi what if I want a program to check if a number is divisble by 2 and 8? and the code written in the question is not done?

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