【问题标题】:x86 assembly print register asciix86 汇编打印寄存器 ascii
【发布时间】:2013-08-24 01:37:42
【问题描述】:

我意识到有很多关于如何以十进制、ASCII 形式输出整数的问题/答案。我已经采取了一些代码并根据自己的需要对其进行了修改,但它不仅打印数字,还继续打印乱码,并且 windows 告诉我程序停止工作。我认为问题在于它会不断弹出堆栈的值,即使它应该跳出循环。以下是完整代码:

.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
include \masm32\include\masm32.inc 
includelib \masm32\lib\kernel32.lib 
includelib \masm32\lib\masm32.lib 

.data
base dd 10
ans dd ?

.code
start:
MOV ECX,3    ;I'm writing a compiler using
PUSH ECX     ;Jack Crenshaw's "Let's Build A Compiler!"
MOV ECX,9    ;This is just some sample output that I put in
ADD ECX,[ESP];The answer that prints out should be 42
PUSH ECX
MOV ECX,2
XOR EDX,EDX
POP EAX
IDIV ECX
MOV ECX,EAX
PUSH ECX
MOV ECX,7
IMUL ECX,[ESP]

mov eax,ecx
xor ecx,ecx
separateDigit: 
xor edx,edx
idiv base
push edx
inc ecx
cmp eax,0
jne separateDigit

printDigit: 
mov ans,0
pop ans
dec ecx
add ans,'0'
invoke StdOut,addr ans
cmp ecx,0
jne printDigit

invoke ExitProcess, 0
end start

有几个小时没盯着它看的人可以插话告诉我我做错了什么吗?

【问题讨论】:

  • 您知道invoke StdOut 是否触及您关心的任何寄存器,例如ecx?也许之前的push ecxinvoke 之后的pop ecx 可以解决它?
  • @mbratch 是的,谢谢。我只是在阅读有关(大多数)寄存器如何对任何调用公平游戏但它只是没有点击。

标签: assembly ascii masm


【解决方案1】:

invoke 调用可能不是“注册安全”,因此您需要保留您的 ecx 值:

printDigit: 
    mov ans,0
    pop ans
    dec ecx
    add ans,'0'
    push ecx     ; save ecx
    invoke StdOut,addr ans
    pop ecx      ; restore ecx
    cmp ecx,0
    jne printDigit

【讨论】:

    猜你喜欢
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    相关资源
    最近更新 更多