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


問題描述

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

我正在用彙編語言製作一個程序,我試圖通過將字符推入堆棧來在控制台上顯示一個字符。我正在使用中斷 21H 的服務號 2 來打印字符。

當我運行程序時,字符顯示在控制台上,但問題是它在控制台上顯示無限次。我沒有使用任何循環,所以不知道為什麼它會在控制台上無限次打印。

需要幫助來解決問題。

我正在使用emu8086。

.model small
.data
st1 db "A",'$' 
.code
main proc
    mov AX,@data
    mov DS, AX

    mov AX, offset st1
    push AX

    call putch

    putch proc near
        mov BP,SP 
        mov DX,[BP+2]
        mov AH,2
        INT 21H
        ret
    putch endp 

    mov AH,4CH
    INT 21H

end main 

參考解法

方法 1:

Next image illustrates the problem :

  • When you call putch the program jumps as the red arrow.
  • When putch finishes it returns to the call as the green arrow.
  • Then the program continues (blue arrow) and executes the next line, which is putch again (that's a loop).

enter image description here

The solution is to move above the block that finishes the program. Another problem is that your program requires a stack. Finally, @MargaretBloom is right (as usual), your code is pushing the address of the string, but you are using int 21h ah=2 to display the char, so there are two options : #1 push the char and display with ah=2, or, #2 push the address and display with ah=9 :

#1

.model small
.stack 100h              ;◄■■ STACK!!!
.data
st1 db "A",'$' 
.code
main proc
    mov AX,@data
    mov DS, AX

    xor ax, ax           ;◄■■ CLEAR AX.
    mov AL, st1          ;◄■■ CHAR IN AX.
    push AX              ;◄■■ PUSH THE CHAR.

    call putch

    mov AH,4CH           ;◄■■ FINISH PROGRAM HERE.
    INT 21H

    putch proc near
        mov BP,SP 
        mov DX,[BP+2]    ;◄■■ THE CHAR.
        mov AH,2         ;◄■■ 2 = DISPLAY ONE CHAR.
        INT 21H
        ret
    putch endp 

end main 

#2

.model small
.stack 100h              ;◄■■ STACK!!!
.data
st1 db "A",'$' 
.code
main proc
    mov AX,@data
    mov DS, AX

    mov AX, offset st1   ;◄■■ GET THE ADDRESS.
    push AX              ;◄■■ PUSH THE ADDRESS.

    call putch

    mov AH,4CH         
    INT 21H

    putch proc near
        mov BP,SP 
        mov DX,[BP+2]    ;◄■■ GET THE ADDRESS.
        mov AH,9         ;◄■■ 9 = DISPLAY STRING '$' TERMINATED.
        INT 21H
        ret
    putch endp 

end main 

(by YousafJose Manuel Abarca Rodríguez)

參考文件

  1. putch function in assembly language through stack (CC BY‑SA 2.5/3.0/4.0)

#emu8086 #assembly #x86 #dos






相關問題

使用 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?)







留言討論