I'm practicing some basic assembly online and I'm very new in this field so I tried writing the equivalent of the following C program:
int a,b,c;
main () {
int d;
a=28;
b=a+31;
c=14;
if (a>23) {
c=a%(b&a%10)/c;
}
d=c;
}
Here's what I did:
segment .text
global _start
_start:
mov eax, 28 ; a=28
mov ebx, 0 ; b=0
mov ebx, eax; b=28
add ebx, 31; b=28+31
mov ecx, 14; c=14
;
cmp eax, 23 ; a>23
jnle thenblock; there's no else block so we jump straight into thenblock if a>23
;
thenblock:
and eax, ebx ; a=b&a?
mov esi, eax ; save the value of eax because when we perform modulo it will be overwritten
mov edx, 0 ; our remainder will be placed here
mov ebx, 10 ; we divide by 10
div ebx ; divide eax by ebx, where the remainder will be placed in edx;
;
mov eax, esi ; we restore eax
mov ebx, edx ; ebx is (b&a%10), we divide eax by it
mov edx, 0 ; our remainder will be placed here
div ebx
;
mov ebx, edx ; we move a%(b&a%10) to ebx
mov eax, esi ; we restore eax again
div ebx ; divide eax by ebx
;
next:
mov edx, eax
ret
Now, I'm fairly certain that this has multiple errors which I don't know how to fix. I'd be very thankful if someone could point out what mistakes I made and how to fix them.
NOTE: I used a version of a nasm compiler with GoLinker. I compiled using
nasm -f win32 sample.asm