【发布时间】:2017-02-02 15:25:46
【问题描述】:
下面我尝试在 32 位汇编中实现的 C++ 代码是这样编写的:
for(int ebx = 3; ebx < 10; ebx++){
print("LO");
for(int esi = 2; esi < ebx; esi++){
PRINT("L1");
for(int ebp = 0; ebp < esi; ebp++){
PRINT("L2");
}
}
}
这是我的汇编代码:
SECTION .data ; Section containing initialized data
helloWorld0: dw "L1",10,0
helloWorld1: dw "L2",10,0
helloWorld2: dw "L3",10,0
SECTION .bss ; Section containing uninitialized data
SECTION .text ; Section containing code
extern printf ; Print function from glibc
global main ; Linker needs this to find the entry point
main:
nop ; This no-op keeps gdb happy
push ebp ; Set up stack frame for debugger
mov ebp,esp
push ebx ; Must preserve EBP, EBX, ESI & EDI
push esi
push edi
; Everything before this is boilerplate; use it for all apps
mov ebx, 3 ;L1
mov esi, 2 ;L2
mov ebp, 0 ;L3
L1:
push helloWorld0
call printf
L2:
push helloWorld1
call printf
L3:
push helloWorld2
call printf
inc ebp
cmp ebp, esi
jne L3
inc esi
cmp esi, ebx
jne L2
mov esi, 2
inc ebx
cmp ebx, 10
jne L1
; Everything after this is boilerplate; use it for all apps
pop edi ; Restore saved registers
pop esi
pop ebx
mov esp,ebp ; Destroy stack frame before returning
pop ebp
ret ; Return control to Linux
似乎代码永远无法确定何时 ebp = esi。我是汇编语言的新手,所以我的教授提供了样板文件。我使用了 EBP、ESI 和 EBX,因为它们被保留以供使用。关于导致第三个嵌套循环的无限循环的任何想法?
【问题讨论】:
-
在循环内逐行调试时发现了什么?
-
我用linux终端编码,需要什么软件调试?
-
您的任务似乎与此类似:stackoverflow.com/questions/39680843/…。我那里的一些 cmets 将适用于此。
-
使用 gdb 在 Linux 文本外壳中进行调试,就像 Michael 在 cmets 中对您上一个问题提出的建议一样。请参阅x86 tag wiki 的底部以了解有关将其用于 asm 的一些提示。
-
@Michael Petch,嘿,感谢您对我上一个问题的帮助!它把我带到了我的程序的这一点!