3

Is it possible to mimic an if-statment in x86 assembly language (using masm syntax)? I want to do something like this in x86 assembly language, but I'm not sure which operator I should use to mimic an if-else statement. Should I use the jl instruction, or the cmp instruction, or some other instruction?

int i = 2;
int j = 3;
if(i > j){
    i = 1;
}
else{
    i = 4;
}
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • I think I'd either need to use a `cmp` or `jmp` or `j1` instruction to compare each of the two variables, but I'm still not sure which one to use. – Anderson Green Feb 20 '13 at 17:30
  • I still haven't found any concrete examples of if statments using masm syntax - – Anderson Green Feb 20 '13 at 17:36
  • Also, why was this question downvoted? I still haven't found any concrete examples of if-statements in x86 assembly language (using masm syntax). – Anderson Green Feb 20 '13 at 17:40
  • Let me guess, your question has been downvoted for apparent lack of effort. You haven't got the necessary documentation and you haven't studied any assembly tutorials long enough to not ask simple questions like this. – Alexey Frunze Feb 20 '13 at 17:42
  • @AlexeyFrunze I've studied many different assembly language tutorials, but I still don't entirely understand how branch statements work, and how to use them. – Anderson Green Feb 20 '13 at 17:43
  • There are other tutorials probably? And perhaps you could spend more time studying them? And perhaps you could try things and see what happens? And identify that which you don't understand at least? And then the CPU manual describes how each and every instruction works. – Alexey Frunze Feb 20 '13 at 17:47
  • This might be relevant (related to jump instructions): http://stackoverflow.com/questions/4557279/assembly-jle-jmp-instruction-example – Anderson Green Feb 20 '13 at 17:58

5 Answers5

8

A combination of cmp and jcc (that is, conditional jump) instructions will do. Look up your CPU manual.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • Specifically, I'm using the masm assembler on Windows - which CPU manual should I refer to, in this case? – Anderson Green Feb 20 '13 at 17:33
  • 1
    Intel or AMD, you choose. For MASM itself you need a separate one. – Alexey Frunze Feb 20 '13 at 17:40
  • I found [this reference for x86 assembly language](http://www.fermi.mn.it/linux/quarta/x86/jcc.htm), but I can barely make sense of it. What does "jump short" mean in this case? – Anderson Green Feb 20 '13 at 19:22
  • x86 doesn't mean you should check out a processor from 1986. :-) Instead try [some original Intel manuals](http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html) directly from the source. – Bo Persson Feb 20 '13 at 19:30
  • Hint1: on every line with `short` there's `rel8`. On all others there's `rel16/32`. Hint2: in the description there're numbers like `127`, `32767` and `2^31`, can you see a correlation? Hint3: `cb` vs `cw` vs `cd`. Hint4: `EIP := EIP + SignExtend(rel8/16/32)` Hint5: go to `developer.intel.com` and find there `Intel® 64 and IA-32 Architectures Software Developer’s Manual Combined Volumes: 1, 2A, 2B, 3A and 3B`. Once found, look up those `cb` and `rel8` things in the document. – Alexey Frunze Feb 20 '13 at 19:45
  • 1
    Since you're using MASM, why not just use the .if macro... :-) – Brian Knoblauch Feb 20 '13 at 19:56
3

You could look up your CPU manual, or you could just ask the compiler

gcc -c foo.c
objdump -d foo.o

Where foo.c is just your function in a simple method. The output is

00000000 <_foo>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 10                sub    $0x10,%esp
   6:   c7 45 fc 02 00 00 00    movl   $0x2,-0x4(%ebp)
   d:   c7 45 f8 03 00 00 00    movl   $0x3,-0x8(%ebp)
  14:   8b 45 fc                mov    -0x4(%ebp),%eax
  17:   3b 45 f8                cmp    -0x8(%ebp),%eax
  1a:   7e 09                   jle    25 <_foo+0x25>
  1c:   c7 45 fc 01 00 00 00    movl   $0x1,-0x4(%ebp)
  23:   eb 07                   jmp    2c <_foo+0x2c>
  25:   c7 45 fc 04 00 00 00    movl   $0x4,-0x4(%ebp)
  2c:   c9                      leave
  2d:   c3                      ret
  2e:   90                      nop
  2f:   90                      nop

The stuff at the start is setting up the stack / dealing with the calling convention, the important bit is this

  17:   3b 45 f8                cmp    -0x8(%ebp),%eax
  1a:   7e 09                   jle    25 <_foo+0x25>
  1c:   c7 45 fc 01 00 00 00    movl   $0x1,-0x4(%ebp)    // i = 1;
  23:   eb 07                   jmp    2c <_foo+0x2c>
  25:   c7 45 fc 04 00 00 00    movl   $0x4,-0x4(%ebp)    // i = 4;

In this case its just a cmp, followed by a jle - the "if" part of the statement ends with a jmp to skip over the else part of the statement.

Justin
  • 84,773
  • 49
  • 224
  • 367
1

Assuming you have two assembly labels:

cmp j1, j2
jg LABEL1
LABEL2

And MASM doesn’t “have syntax”, it is like a “compiler”, which named usually as Assembler.


side note:

Assembly is the language and Assembler is the “compiler”.

0x90
  • 39,472
  • 36
  • 165
  • 245
  • 1
    I'm confused now - how is it possible for a compiler (or assembler) to not have syntax? – Anderson Green Feb 20 '13 at 17:38
  • 1
    @AndersonGreen `MASM` is the compiler, it uses [Intel](http://en.wikipedia.org/wiki/X86_assembly_language) syntax. – Justin Feb 20 '13 at 17:45
  • 1
    @Justin: MASM is an *assembler*, not a compiler. It uses it's own flavour of Intel syntax, subtly different from NASM for example, so yes it is normal to talk about MASM syntax. (e.g. `mov eax, label` in NASM vs. `mov eax, OFFSET label` in MASM are equivalent.) – Peter Cordes Sep 06 '21 at 11:24
0

This might help for a better understanding of how to implement IF statements in assembly. Note that it is not in MASM syntax.

Complex IF statement in Assembly

Community
  • 1
  • 1
Mahdi
  • 9,247
  • 9
  • 53
  • 74
0

if your using masm32 you can write if statements just like in c++ example

.if eax == 0
invoke MessageBoxA, NULL, addr strn, offset title, NULL
.endif