0

I know this might sound stupid, but I'm new to assembly language, so please bear with me!

I have following assembly code, which is the simplified version of what I was trying to do.

 1  # print.s
 2  # C callable: char* print()
 3  
 4  .data
 5  output:
 6          .asciz "abcd"
 7          
 8  .text
 9  .globl _printbin
10  
11  _printbin:
12          pushl %ebp              # set up stack frame
13          movl %esp, %ebp         # save esp in ebp
14  
15          movl $output, %eax      # put the address of "abcd" in eax                              
16                  
17          xor %ebx, %ebx                  # clear ebx
18          movl $5, %ebx                   # put 5 in ebx (input for func)
19          movl $0, %edx                   # put 1 in edx (index)
20          jmp _func                       # call func
21                  
22  back1:                  
23          xor %ebx, %ebx                  # clear ebx
24          movl $7, %ebx                   # put 7 in ebx (input for func)
25          movl $2, %edx                   # put 2 in edx (index)
26          jmp _func                       # call func
27                  
28  end:            
29          movl %ebp, %esp                 # restore esp
30          popl %ebp                       # restore ebp
31          ret
32  
33  # take the input, add 1 to it, 
34  # then print it to eax at the specified index
35  _func:                                  # num input in %ebx, index is in %edx , print to: %eax
36          addb $0x1, %ebx                 # print the result to eax 
37          movb %ebx, (%eax, %edx)
38          jmp back1                       # how to decide wether to jump to back1 or to end?
39                    
40  .end
41  

THe question is, how do I jump to a some kind of "variable" label. (So sometimes I want to jump to this label, but some other times another label... that kind of idea.)

Hawken
  • 2,059
  • 19
  • 34
user113454
  • 2,273
  • 9
  • 28
  • 35
  • why are you using `addb` and `movb` on lines 36, 37; this should be `addl` and `movl` if I'm not mistaken; the `b` postfix is for `bytes` – Hawken Apr 22 '12 at 14:33
  • Related: [What does an asterisk \* before an address mean in x86-64 AT&T assembly?](https://stackoverflow.com/q/9223756) re: AT&T syntax for indirect jumps/calls. – Peter Cordes Dec 11 '22 at 21:40

1 Answers1

3

If the address you want to jump to is in a register, you can do an absolute indirect jump:

jmp *%eax

After looking at your code, it looks like you want to perform a conditional jump.

    cmpl %eax, %ebx
    je label1

    ; this is executed if %eax != %ebx

    jmp end

label1:
    ; this is executed if %eax == %ebx

end:
  • It's not exactly like I want to do a conditional jump. It's more of "calling a function". So _func is the part where I put my subroutine. And the subroutine doesn't know where to return to. I need a way to tell it. To put the question another ways, can I some how *store* the value of a label into some register to that I can do the indirect jump like you mentioned? – user113454 Mar 04 '12 at 02:04
  • 2
    There's the `call` instruction, which pushes the return address to the stack and jumps to the address you're calling. –  Mar 04 '12 at 02:06
  • 2
    The `ret` instruction will pop the return address off the stack and jump back to it –  Mar 04 '12 at 02:06