如何使用 call 和 ret 更改堆棧內容? (how to change stack content with call and ret?)


問題描述

如何使用 call 和 ret 更改堆棧內容? (how to change stack content with call and ret?)

這段代碼類似於圖靈機的模擬。我正在檢測此代碼,並且我製作了一張關於更改它的每一步的表格,但我不明白如何使用 CALL AND RET

.model small
.data
bant db 0,0,0,0,0,0,0,0,0

.code
.startup

mov si,4
call stateA
.exit

stateA proc near
cmp bant[si],0
je AB
jmp AC

AB:
mov bant[si],1
inc si
call stateB
jmp RTA

AC:
mov bant[si],1
dec si
call stateC

RTA: ret
stateA endp

stateB proc near
cmp bant[si],0
je BA
jmp BB

BA:
mov bant[si],1
dec si
call stateA
jmp RTB

BB:
mov bant[si],1
inc si
call stateB

RTB: ret
stateB endp
stateC proc near
cmp bant[si],0
je CB
jmp CHLT

CB:
mov bant[si],1
dec si
call stateB
jmp RTC

CHLT:
mov bant[si],1
inc si

RTC: ret
stateC endp

end
更改堆棧內容

參考解法

方法 1:

RET doesn't write to the stack, but it does modify SP. CALL writes a return address to the stack, as well as modifying SP.

The only value you can write to the stack is the IP of the instruction after the CALL, so I don't think it's possible to do very much with just CALL and RET instructions.

You're probably going to need to do it the normal way with MOV instructions and other instructions. Usually with addressing modes relative to [BP], after making a stack frame.

方法 2:

There's neat trick how to load offset of some value on top of stack:

    call print_message   ; address of string is pushed on top of stack
    db "some text message to print",0  ; defined inside code
print_message:
    call some_print_function ; which want single argument on stack as "ptr to string"
    ; restore stack as needed (but don't do "ret", it would execute string)

But this is very likely of more usage in 32b mode, as the pushed offset is inside code segment cs, so in 16b real mode this would work well (conveniently) only with code which has everything in single segment and cs = ds. For example most of the ".com" executables would fit this description.

If your code is using more segments, then that print routine has to be written in a way to address that string through cs:, which is not very common.

(by mucoPeter CordesPed7g)

參考文件

  1. how to change stack content with call and ret? (CC BY‑SA 2.5/3.0/4.0)

#emu8086 #assembly






相關問題

使用 include 'emu8086.inc' 反轉字符串的大小寫和順序 (Reverse case and order of a String using include 'emu8086.inc')

emu8086 : ARR 不包含任何值 (emu8086 : ARR do not contain any value)

為什麼這個彙編代碼不刪除擴展名為“.lnk”的文件? (Why doesn't this assembly code delete the file with ".lnk" extension?)

將 C 代碼轉換為 8086 程序集 (Converting C code into 8086 assembly)

循環無法打印字母 (Loop does not work to print alphabets)

如何使用 call 和 ret 更改堆棧內容? (how to change stack content with call and ret?)

彙編語言中的putch函數通過堆棧 (putch function in assembly language through stack)

如何在不使用 HLT 的情況下對程序進行 HLT (How to HLT the program without using HLT)

使用 8086 彙編計算 10 的階乘 (Computing the factorial of 10 using 8086 assembly)

你好,我有一個問題,我需要從用戶那裡得到輸入,輸入是一個數字和一個數字後的數字,數字可以是雙字 (hello , i got a question that i need to get input from user, the input is a number and digit after digit ,the number can be a doubleWord)

在彙編語言中得到錯誤的結果 (Getting wrong result in Assembly language)

我的氣泡代碼是彙編語言嗎? (is my code for bubble right at assembly language?)







留言討論