-1

I am a beginner (first year in UNI). I use VisualStudio and the x86 configuration. This is my function:

int hex(int x) {
    __asm {
        MOV EAX, x
        CMP EAX, 9
        JG jumpto
        ADD EAX, 48
        MOV x, EAX
        jumpto: 
        ADD EAX, 55
        MOV x, EAX
        }
    return x;
}

There is an issue with the JG condition or I dont understand it right. My understanding of the CMP and JG lines is:

  1. EAX is compared to 9 (x to 9).
  2. IF it is greater than 9 it'll jump to "jumpto" (wow that sounds stupid).
  3. IF it is NOT greater, it just reads the line between JG and jumpto. So I basically think it works like if/else

My problem is that, if it is not greater the program still reads the line that are in the "jumpto" segment (ADD EAX,55 ; MOV x, EAX) which by my understanding should't do.

Can someone explain to me (like I'm a child or as easy as possible) why it doesn't work. I am hopeles. Thank you.

I tried google and again if I'm not making a mistake it just made me more confident in the if/else thing.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
pnd__
  • 11
  • 2
    After adding 48, it will continue on and add 55, since you never told it not to. You would need an unconditional jump (`jmp`) before the `jumpto` label – ChrisMM Mar 18 '23 at 19:44
  • 1
    Aside: never use variable or label names that do not give added value. Use something that is descriptive of *why* you make that jump if there is no comment explaining it. For eaxmple `JG ishexdig` – Weather Vane Mar 18 '23 at 19:56
  • Other than Jerry's suggestion of simplifying it to an `if()` and unconditional add, duplicate of [Code executes condition wrong?](https://stackoverflow.com/q/32872539) linked from the FAQ section of https://stackoverflow.com/tags/x86/info . Stack Overflow doesn't make it easy for people to find the tag wikis, but there's good stuff in the x86 one. Including that and others that explain that execution falls through to the next instruction unless there's a jump instruction, regardless of labels; think about how the machine code will be laid out. – Peter Cordes Mar 19 '23 at 04:14

2 Answers2

3

As @ChrisMM pointed out in the comment, what you have is roughly equivalent to:

if (x<=9) {
   x += 48;
}
x += 55;

..whereas presumably you want something more like:

if (x<=9) {
    x += 48;
} else {
    x += 55;
}

As he pointed out, you can get this by adding an unconditional jump just before the jumpto label you have right now, so adding 48 and adding 55 happen exclusively of each other.

Another possibility that's often a bit more efficient would be equivalent to something like this:

if (x > 9) {
    x += (55-48);
}
x += 48;

This becomes something like:

cmp eax, 9
jle  jumpto
add eax, (55-48)
jumpto:
add eax, 48
mov x, eax
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

So I basically think it works like if/else

That is correct, but even an high level language would have a jump of some kind right before the else, so the execution could continue at the endif:

if expression
  action1
  ; Here is a hidden jump
else
  action2
endif

Now in assembly you have to write that jump yourself:

    MOV EAX, x
    CMP EAX, 9
    JG  jumpto
    ADD EAX, 48
    jmp TheEndif
jumpto: 
    ADD EAX, 55
TheEndif:
    MOV x, EAX

A simpler method:

    mov  eax, x
    cmp  eax, 9
    jbe  TheEndif
    add  eax, 7
TheEndif:
    add  eax, 48
    mov  x, eax
Sep Roland
  • 33,889
  • 7
  • 43
  • 76