Assembly jmp至';变量';组件中的标签(AT&;T语法)
我知道这听起来可能很愚蠢,但我对汇编语言还不熟悉,所以请容忍我 我有下面的汇编代码,这是我试图做的简化版本Assembly jmp至';变量';组件中的标签(AT&;T语法),assembly,subroutine,att,Assembly,Subroutine,Att,我知道这听起来可能很愚蠢,但我对汇编语言还不熟悉,所以请容忍我 我有下面的汇编代码,这是我试图做的简化版本 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
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
问题是,我如何跳转到某种“变量”标签。(因此,有时我想跳转到这个标签,但有时我想跳转到另一个标签……这种想法。)如果要跳转到的地址在寄存器中,可以执行绝对间接跳转:
jmp *%eax
查看代码后,您似乎希望执行条件跳转
cmpl %eax, %ebx
je label1
; this is executed if %eax != %ebx
jmp end
label1:
; this is executed if %eax == %ebx
end:
如果要跳转到的地址位于寄存器中,则可以执行绝对间接跳转:
jmp *%eax
查看代码后,您似乎希望执行条件跳转
cmpl %eax, %ebx
je label1
; this is executed if %eax != %ebx
jmp end
label1:
; this is executed if %eax == %ebx
end:
这和我想做条件跳转不完全一样。更多的是“调用函数”。所以_func是我放置子程序的部分。子例程不知道返回到哪里。我需要一个方法来告诉你。换一种方式来说,我能告诉你如何将标签的值存储到某个寄存器中,以便像你提到的那样进行间接跳转吗?这里有
call
指令,这会将返回地址推送到堆栈并跳转到您正在调用的地址。ret
指令会将返回地址从堆栈中弹出并跳转回堆栈。这与我想做的条件跳转完全不同。更多的是“调用函数”。所以_func是我放置子程序的部分。子例程不知道返回到哪里。我需要一个方法来告诉你。换一种方式来说,我能告诉你如何将标签的值存储到某个寄存器中,以便像你提到的那样进行间接跳转吗?这里有call
指令,它将返回地址推送到堆栈并跳转到您正在调用的地址。ret
指令将从堆栈中弹出返回地址并跳回堆栈。为什么在第36、37行使用addb
和movb
;如果我没弄错的话,这应该是addl
和movl
;b
后缀表示字节
为什么在第36行和第37行使用addb
和movb
;如果我没弄错的话,这应该是addl
和movl
;b
后缀表示字节