2012-03-04 88 views
0

我知道這聽起來很愚蠢,但我對彙編語言很陌生,所以請耐心等待!jmp在程序集中的'變量'標籤(AT&T語法)

我有以下彙編代碼,這是我試圖做的簡化版本。

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 

問題是,我該如何跳轉到某種「變量」標籤。 (所以有時候我想跳到這個標籤上,但有些時候是另一個標籤......這種想法)

+0

你爲什麼在第36,37行使用'addb'和'movb';如果我沒有弄錯,這應該是'addl'和'movl'; 'b'後綴用於'bytes' – Hawken 2012-04-22 14:33:22

回答

3

如果你想跳轉到的地址在寄存器中,你可以做一個絕對間接跳轉:

jmp *%eax 

查看你的代碼後,它看起來像你想執行條件跳轉。

cmpl %eax, %ebx 
    je label1 

    ; this is executed if %eax != %ebx 

    jmp end 

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

end: 
+0

這不完全像我想做一個條件跳轉。這更多的是「調用一個函數」。所以_func是我放置子程序的部分。子程序不知道要返回的位置。我需要一種方式來告訴它。爲了用另一種方式來解決這個問題,我可以如何將一個標籤的價值存儲到某個寄存器中,以便我可以像你所提到的那樣間接跳轉? – user113454 2012-03-04 02:04:42

+2

有'調用'指令,它將返回地址推入堆棧並跳轉到您要調用的地址。 – 2012-03-04 02:06:13

+2

'ret'指令將彈出堆棧的返回地址並跳轉回去 – 2012-03-04 02:06:40