I'm trying to get into assembler and I'm reading through a document that gives a short introduction. There is a task to convert the following code to pure assembler code:
mov bx, 30
if (bx <= 4) {
mov al , ’A’
} else if (bx < 40) {
mov al, ’B’
} else {
mov al, ’C’
}
mov ah, 0x0e
int 0x10 ; print the character in al
jmp $
; Padding and magic number.
times 510-($-$$) db 0
dw 0xaa55
I created this assembler code:
mov bx, 30
cmp bx, 4
jle if_branch
cmp bx, 40
jl elif_branch
mov al, 'C'
if_branch:
mov al, 'A'
elif_branch:
mov al, 'B'
mov ah, 0x0e
int 0x10
jmp $
times 510-($-$$) db 0
dw 0xaa55
However no matter what I put into bx in line 1 the output will always be 'B'. What am I missing there?
My setup: I'm writing the assembler code in codeblocks and use nasm to create a bin file. Then I execute that bin file using qemu.
SOLUTION thanks to ecm and barny in the comments: The problem was that after the mov al, 'A' in the if_branch label the execution would just continue and execute mov al, 'B'. Defining an empty label in the end and jumping to it solved the problem. Together with some additional improvements this is the final code:
mov bx, 40
mov al, 'A'
cmp bx, 4
jle the_end
mov al, 'B'
cmp bx, 40
jl the_end
mov al, 'C'
the_end:
mov ah, 0x0e
int 0x10
jmp $
times 510-($-$$) db 0
dw 0xaa55