I have only just started to learn the assembly language for the x86 architecture. I am doing some exercises based on conditional branching. At this point in time, I have been introduced to the JMP, JZ and JNZ instructions as far as branching of code is concerned. This is the link for the exercise. Point 5 says if input is given as 0
, the program screws up. That's fair, since dec eax
would result in the eax
register to hold 0xffffffff
. I need to fix this. Here is what I have come up with -
Add a dummy instruction add eax, 0h
just to check if this results in the zero-flag
being set, and if set jump to a place in the code, which prints out the required value and exit the program.
start:
; The program begins here:
call read_hex
add eax, 0h; my code
jz lb2; my code
mov ecx,1
lb1:
add ecx,ecx
dec eax
jnz lb1
mov eax,ecx
jmp lb3
lb2:
inc eax
call print_eax
; Exit the process:
push 0
call [ExitProcess]
lb3:
call print_eax
; Exit the process:
push 0
call [ExitProcess]
The thing that's making me uncomfortable is that I have duplicate code in lb2
and lb3
and I am not quite sure if there is any way around this.