I have problem with my code. this code in c++ i have to write in assembly. i try to write but it doesnt work

for (i=1; i<11; i++)
if(i%2 == 0)
sum = sum +i

mov cl, 02d
mov ax, 0d
mov bh, 0d
check:
inc al
mov dl, al
div cl
cmp ah, 0d
je sum
cmp ax, 11d
jne check


sum:
add bh, al
jmp check

The previous contents of the accumulator AX is destroyed on each
execution of DIVide. Hence your are dividing your divisor by your
previous remainder and quotient in AX. That seems to me a bug.

An 8-bit divisor will divide against a 16-bit Implicit Dividend in AX,
after the division AH will contain the remainder and AL the quotient.

MOV AX, 1
MOV CL, 2
myloop:
PUSH AX
DIV CL ; Divide 8-bit CL by 16-bit dividend in AX, AH=Remainder AL=Quotient
CMP AH, 0 ; Is remainder 0? i%2=0, percentage is modulus operator in C/C++
JZ Sum
POP AX ; Restore contents of AX, 1 on first loop
INC AX
CMP AX, 11
JB myloop
Sum:
; AL will contain quotient
ADD [MySum], AL

tnx for your help, it was very useful for me. really works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.