assembly 在X64 MASM中使用printf / scanf

yzxexxkh  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(166)

这款MASM X64出自教科书。我无法在Visual Studio中编译它。我用Visual Studio成功地编写和编译了MASM X32程序printf/scanf。我不知道我是否在X64中调用了printf/scanf错误,或者有MASM编译错误问题。它为printfscanf提供了一个“未解析的外部符号”错误。任何洞察力都会有所帮助。

; Section 11.9 Complete Program

printf    PROTO     
scanf     PROTO  
        .data
msg1fmt   byte "%s",0
msg2fmt   byte 0Ah,"%s",0Ah,0Ah,0
msg3fmt   byte "   %lld", 0Ah,0Ah,0
in1fmt    byte "%lld",0
msg2      byte "Enter an integer: ",0
msg3      byte "Reversed",0
n         sqword 5
arry      sqword 5 dup(?)
        .code
main      proc
        mov rcx,n                     ; initialize rcx to n
        mov rbx,0                     ; initialize rbx to 0
for01:    nop
        push rcx                      ; save rcx
        lea rcx, msg1fmt
        lea rdx, msg2
        sub rsp, 40
        CALL printf
        add rsp, 40
        lea rcx, in1fmt
        lea rdx,arry[rbx]
        sub rsp,40
        call scanf
        add rsp,40
        pop rcx                       ; restore rcx
        add rbx,8                     ; increment rbx by 8
        loop for01
endfor01: nop

        lea rcx, msg2fmt
        lea rdx, msg3
        sub rsp, 40
        CALL printf
        add rsp, 40
        mov rcx,n                     ; initialize rcx to n
        sub rbx,8                     ; subtract 8 from rbx

for02:    nop
        push rcx                      ; save rcx
        lea rcx, msg3fmt
        mov rdx, arry[rbx]
        sub rsp, 40
        CALL printf
        add rsp, 40
        pop rcx                       ; restore rcx
        sub rbx,8                     ; decrement rbx by 8
        loop for02
endfor02: nop

        ret
main      endp
        end

字符串

wsxa1bj1

wsxa1bj11#

printfscanf是C运行时库libcmt.lib的一部分,您需要关键字extern将它们声明为外部过程。

reverse.asm

option casemap:none

includelib kernel32.lib
includelib user32.lib
includelib libcmt.lib
includelib legacy_stdio_definitions.lib

extern printf:proc
extern scanf:proc

; Section 11.9 Complete Program

        .data
msg1fmt   byte "%s",0
msg2fmt   byte 0Ah,"%s",0Ah,0Ah,0
msg3fmt   byte "   %lld", 0Ah,0Ah,0
in1fmt    byte "%lld",0
msg2      byte "Enter an integer: ",0
msg3      byte "Reversed",0
n         sqword 5
arry      sqword 5 dup(?)
        .code
main      proc
        mov rcx,n                     ; initialize rcx to n
        mov rbx,0                     ; initialize rbx to 0
for01:    nop
        push rcx                      ; save rcx
        lea rcx, msg1fmt
        lea rdx, msg2
        sub rsp, 40
        CALL printf
        add rsp, 40
        lea rcx, in1fmt
        lea rdx,arry[rbx]
        sub rsp,40
        call scanf
        add rsp,40
        pop rcx                       ; restore rcx
        add rbx,8                     ; increment rbx by 8
        loop for01
endfor01: nop

        lea rcx, msg2fmt
        lea rdx, msg3
        sub rsp, 40
        CALL printf
        add rsp, 40
        mov rcx,n                     ; initialize rcx to n
        sub rbx,8                     ; subtract 8 from rbx

for02:    nop
        push rcx                      ; save rcx
        lea rcx, msg3fmt
        mov rdx, arry[rbx]
        sub rsp, 40
        CALL printf
        add rsp, 40
        pop rcx                       ; restore rcx
        sub rbx,8                     ; decrement rbx by 8
        loop for02
endfor02: nop

        ret
main      endp
        end

字符串
上面的代码是使用这些命令编译和链接的:

@echo on

if not defined DevEnvDir (
  call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat"
)

ml64.exe /c reverse.asm

link.exe /SUBSYSTEM:console /LARGEADDRESSAWARE:NO reverse.obj


运行程序输出


的数据

相关问题