gpt4 book ai didi

windows - 使用 MASM 将寄存器输出到控制台

转载 作者:可可西里 更新时间:2023-11-01 11:37:44 25 4
gpt4 key购买 nike

有一天我开始学习 ASM,我已经完成了一些教程,甚至成功地修改了教程内容以使用 jmp 和 cmp 等代替 MASM .if 和 .while 宏。

在我继续更高级的教程之前,我决定先尝试编写一些非常非常简单的东西。我正在写一个斐波那契数生成器。这是我到目前为止的来源:

.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

.code
start:

mov eax, 1
mov ecx, 1

_a:

push eax
add eax, ecx
pop ecx

; Jump to _b if there is an overflow on eax

; Print Values Here

jmp _a

_b:

push 0
call ExitProcess

end start

我打算检查 eax/ecx 上的溢出,但现在我只想在屏幕上显示 eax/ecx 的值。

我知道如何从 .data 中推送常量字符串的地址并调用 StdOut,这是 hello world 教程中的第一个示例,但这看起来很不一样(?)。

最佳答案

微软自己提供了这段代码

http://support.microsoft.com/kb/85068

请注意,此代码在 16 位系统上输出 AX 寄存器。但是你可以明白这个想法,你只需要通过循环遍历每个字符将 AX 值转换为 ASCII 字符。跳过中断部分并使用 StdOut 函数。

 mov dx, 4          ; Loop will print out 4 hex characters.
nexthex:
push dx ; Save the loop counter.
mov cl, 4 ; Rotate register 4 bits.
rol ax, cl
push ax ; Save current value in AX.

and al, 0Fh ; Mask off all but 4 lowest bits.
cmp al, 10 ; Check to see if digit is 0-9.
jl decimal ; Digit is 0-9.
add al, 7 ; Add 7 for Digits A-F.
decimal:
add al, 30h ; Add 30h to get ASCII character.

mov dl, al
;Use StdOut to print value of dl
;mov ah, 02h ; Prepare for interrupt.
;int 21h ; Do MS-DOS call to print out value.

pop ax ; Restore value to AX.
pop dx ; Restore the loop counter.
dec dx ; Decrement loop counter.
jnz nexthex ; Loop back if there is another character
; to print.

也请看这里:

http://www.masm32.com/board/index.php?PHPSESSID=fa4590ba57dbaad4bc44088172af0b49&action=printpage;topic=14410.0

关于windows - 使用 MASM 将寄存器输出到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7087020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com