We are tasked to do compare integer values and print out appropriate prompt to show which values are greater.
The code I made below initializes 'i' and 'j' to 5 and 4, respectively. The objective is to compare "variables" instead of the immediate values itself. So in this example, I put 'i' and 'j' in the cmp, and not 5 and 4.
global _main
extern _printf, _system
section .text
_main:
; clear screen
push clr
call _system
add esp, 4
;Test prints out i. Successfully does the job.
push dword [i] ; why did it work here and not in *cmp*?
push prompt2
call _printf
add esp, 8
;compare values and do a conditional jump.
CMP dword [i], dword [j] ; compares values stored in i and j. i and j are base 10 nums. This is where the error appears.
JG igreat ; jumps if i is greater than j.
JL jgreat ; jumps if j is greater than i.
JE eks ; jumps if i and j are equal.
ret ; as a measure, this ends the code.
igreat: ; prints out a prompt if i is greater than j.
push dword [j]
push dword [i]
push ibig
call _printf
add esp, 16
ret
jgreat: ; prints out a prompt if j is greater than i.
push dword [i]
push dword [j]
push jbig
call _printf
add esp, 16
ret
eks: ; prints out a prompt if i is equal to j.
push dword [i]
push dword [j]
push ex
call _printf
add esp, 16
ret
last: ; terminates the code
ret
section .data
clr db "cls",0
i dd 5 ; i is initialized to 5
j dd 4 ; j is initialized to 4
prompt2 db "Value is %d",13,10,0
ibig db "Test 1. Comparisons: %d is bigger than %d",13,10,0
jbig db "Test 2. Comparisons: %d is bigger than %d",13,10,0
ex db "Test 3. Comparisons: %d is bigger than %d",13,10,0
Two inquiries I may raise:
- CMP dword [i], dword [j] produces an error: invalid combination of opcode and operands; but I successfully recovered values using the printf functions on the previous call/lines of code. Why is that?
- I tried to replace dword [j] with an immediate, say, CMP dword [i], 9. It does print out the correct prompt, but makes the program not responding. Why is that?
Please be reminded that I am running Windows 8 Intel 32 bit, this code has been "compiled" in NASM running in DoSBox, and GCC in CMD.
I am a total beginner and any help would be much appreciated. Thank you!