0

Here is my awful attempt at converting this:

int a=0;
if(a!=80)
{
a+=1;
}

to assembly (NASM):

push cx
mov cx, 80
loop:
je short endloop
mov al,0xDB
call kputchar
add word [d],1
cmp cx,[d]
endloop:
pop cx

...

d dw 0

I am not very good at ASM, so any help would be much appreciated. TIA

Edited by easy2mem: n/a

2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by gusano79
1

Your original "if" statement mutated into a loop there... let's look at the "if" by itself first.

A basic assembly "if" block might look like this:

cmp a, 80
  jne notequal
  ; a == 80, do something here
  jmp done
notequal:
  ; a != 80, do something else here
done:
  ; program continues...

In your case, it can be a little simpler:

cmp a, 80
  je equal
  inc a  ; only increment when a != 80
equal:
  ; program continues...

You could write your loop using similar code, but the 8086 instruction set includes a LOOP instruction.

A basic assembly "for" loop block might look like this, using CX as your loop counter:

push cx
  mov cx, 80
label:
  ; do something here
  loop label
  pop cx

Note that LOOP counts down to zero.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.