这个汇编过程是如何避免崩溃的?

3

我有一段 Linux nasm 代码,它不会崩溃。在 printString 结束时使用 ret 80 指令,这个程序难道不应该崩溃吗?

bits 32

section .data
    hello:     db 'Hello Linux assembly!!!!!!!!!!!!!!!!!!!',10,0    
    helloLen:  equ $-hello  

    anotherString db "hello im another string!!!!",10,0
    anotherStringlen equ $-anotherString

section .text
    global _start

_start:
    push hello
    push helloLen
    call printString

;;;; should i pop the two paramters I pushed?
;;;; does the ret instruction do it for me?

    push anotherString
    push anotherStringlen
    call printString

    call exit

printString:
    push ebp
    mov ebp, esp

    mov eax, 4
    mov ebx, 1
    mov ecx, [ebp+12] 
    mov edx, [ebp+8]
    int 80h

    pop ebp
    ret 60 ;;;;; How does this not make printString crash?

exit:
    mov eax,1            
    mov ebx,0            
    int 80h
1个回答

5

在汇编语言中做事情不正确并不意味着你一定会崩溃。

ret 60指令在返回后从堆栈中弹出了错误数量的值。然而,接下来你所做的事情并不假设堆栈上有任何有用的值。例如,exit函数并不关心堆栈是否被破坏,并且仍将退出你的进程。


正确,但是printString在另一个printString调用之前被调用了一次。那第二个调用呢?上一次对printString的调用不会使堆栈混乱吗? - TheFuzz
1
当使用ret N指令时,我读到它先返回然后弹出N个字节的堆栈数据。但如果它先返回再弹出东西,它怎么知道返回地址在哪里?这就是为什么我不明白我的程序中的ret 60为什么没有崩溃。操作系统的返回地址不是位于堆栈底部附近吗?在这样一个小程序中,我真的可以在返回地址到达之前有60个字节的数据吗? - TheFuzz
学习汇编语言编程而不使用适当的汇编级调试器将会非常困难。我建议你为你的系统找一个调试器,并花些时间学习它可以展示给你什么。 - Greg Hewgill

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接