0

I'm just trying to compare 2 values and I think the syntax is correct but it doesn't work, same goes with integer.

%include "asm_io.inc"

segment .data
    msg_fail        db  "wrong", 0
    msg_success     db  "right", 0
    value1          db  "A", 0
    value2          db  "z", 0

segment .bss
    

segment .text
    global  asm_main

asm_main:
    
    enter   0,0
    pusha

    mov eax, [value1]
    
    cmp eax, [value2]
    je next

next:
    mov eax, msg_fail
    call print_string
    
    
    popa
    mov     eax, 0
    leave
    ret

I expect that the program does nothing, since A and z are not equal but it always prints the message. I'm starting to get desperate

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    You're comparing the 4 bytes starting at the `value1:` label with the 4 bytes starting at `value2`. Single-step with a debugger and look at registers. (You might change the cmp into `mov ecx, [value2]` / `cmp eax, ecx` to see exactly what you get from loading past the end of what you defined in your `.data` section.) – Peter Cordes Dec 14 '22 at 17:07
  • 2
    Also, your jump goes to the same place whether it's taken or not; the jump target is also the fall-through, the next instruction. – Peter Cordes Dec 14 '22 at 17:09
  • i dont understand what the problem is, the registers are 32bit long as my 2 values and in hexadecimal A and z are different so cmp should compare these with false. but i will try what you said – Timo Neske Dec 14 '22 at 18:00
  • Right, I missed the point of the question at first. My first comment is pointing out that they'll compare false even with `value2: db "A", 0`, so that's another bug. The actual problem is that you seem to expect execution to go somewhere else if the `je` condition is false, but you haven't told it where, and that's the opposite of how conditional branches work. They jump somewhere else if the condition is *true*. – Peter Cordes Dec 14 '22 at 18:11
  • ok so you mean i have to tell, where it should go if the condition is not met? so in assembly you cant write if(condition) { do something} but you HAVE TO say else {do this}? – Timo Neske Dec 14 '22 at 20:17
  • 1
    `je` is an `if(equal) goto`. It's a tool you can use to implement structured programs, e.g. by jumping over an if body that shouldn't run. One of the linked duplicates explains this in detail: [How to use jumps in assembly language to implement if conditions?](https://stackoverflow.com/q/72061257) – Peter Cordes Dec 14 '22 at 20:28

0 Answers0