【问题标题】:Visual Studio 2019 - MASM - 32bit Assembly - Hello WorldVisual Studio 2019 - MASM - 32 位程序集 - Hello World
【发布时间】:2020-11-03 21:57:10
【问题描述】:

我通过“MIPS 汇编”学习了汇编语言编程的概念。 我写了几个程序,比如斐波那契、堆栈相关的东西等等。 现在我想进入下一个级别,用于 .386 指令集的 32 位 Windows 程序集。

这是我已经拥有的。

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, deExitCode:DWORD

.data
msg db "Hello, World!", 0

.code

main PROC


    INVOKE ExitProcess, 0
main ENDP
END main

但是我怎样才能调用像 print_string 这样的系统函数呢?我真的不知所措。 我尝试了几个相关的 SO 答案,但他们使用 NASM,所以它对我不起作用。

【问题讨论】:

标签: assembly x86 visual-studio-2019 masm


【解决方案1】:

启用指令并使用指向字符串的指针调用 printf 的示例 masm 代码。指定旧库是因为从 Visual Studio 2015 开始,printf 和 scanf 现在在编译 C 源文件时内联,而程序集将需要使用旧库。如果程序同时包含 C 和汇编源文件,则不需要旧库。

可能需要自定义构建步骤。创建一个空的 Windows Console 项目,添加程序集源文件,右键单击程序集源文件名,然后属性,设置从构建中排除为否,然后设置自定义构建步骤参数:

用于调试构建:

command line: ml /c /Zi /Fo$(OutDir)\example.obj example.asm
output file:  $(OutDir)\example.obj

对于发布版本,不需要 Zi:

command line: ml /c /Fo$(OutDir)\example.obj example.asm
output file:  $(OutDir)\example.obj

示例源代码:

        .686p                   ;enable instructions
        .xmm                    ;enable instructions
        .model flat,c           ;use C naming convention (stdcall is default)

;       include C libraries
        includelib      msvcrtd
        includelib      oldnames
        includelib      legacy_stdio_definitions.lib    ;for scanf, printf, ...

        .data                   ;initialized data
pfstr   db      "Hello world!",0dh,0ah,0
        .data?                  ;uinitialized data
        .stack  4096            ;stack (optional, linker will default)

        .code                   ;code 
        extrn   printf:near
        public  main

main    proc

        push    offset pfstr
        call    printf
        add     sp,4 

        xor     eax,eax
        ret
main    endp

        end

【讨论】:

    【解决方案2】:

    这是一个基本的 WIN32 控制台模式 HelloWorld! ASM 程序,用 MASM 615 编译,在我的 Win10,64 位系统上运行。您可以参考作者的网站进行任何更改(如果有的话)以使其与 VS2019 等一起编译。

    ; 32-Bit Intel Protected Mode Programming
    ; KIP IRVINE Assembly Language for x86 / Intel-Based Computers. 
    ;
    
    .386
    .model flat,stdcall
    .stack 4096
    
    ; DEFINE/DECLARE necessary WIN32 constants, functions etc.
    ;------------------------------------------------------------
    ; Win32 Console handles
    STD_OUTPUT_HANDLE EQU -11       ; predefined Win API constant
    
    
    ; ALIAS  : The following Win32 API functions have an
    ; extra "A" at the end of their name, 
    ; so they are redefined here with text macros:
    WriteConsole EQU <WriteConsoleA>
    
    
    ;                   FUNCTION PROTOTYPES
    ; -------------------------------------------------------------
    ExitProcess PROTO,          ; exit program
        dwExitCode:DWORD        ; return code
    
    
    GetStdHandle PROTO,         ; get standard handle
        nStdHandle:DWORD        ; type of console handle
    
    
    WriteConsole PROTO,                 ; write a buffer to the console
        handle:DWORD,                   ; output handle
        lpBuffer:PTR BYTE,              ; pointer to buffer
        nNumberOfBytesToWrite:DWORD,    ; size of buffer
        lpNumberOfBytesWritten:PTR DWORD,   ; num bytes written
        lpReserved:DWORD                    ; (not used)
    
    ; User Defined Data Section
    ; ---------------------------------------------------------------
    .data
    mesg1               BYTE    "Hello world!",0dh,0ah,0
    sizeMesg1           = ($-mesg1)-1       ; ex-cluding the sign bit
    BytesWritten        DWORD   0
    ConsoleOutHandle    DWORD   0
    
    .code
    main proc
        
        ; Get Standart Output Handle
        INVOKE GetStdHandle, STD_OUTPUT_HANDLE  ; Get a handle to console SCREEN.
        mov ConsoleOutHandle, eax
        
        ; Write Message to the Handle => CONSOLE
        INVOKE WriteConsole, ConsoleOutHandle, ADDR mesg1, (LENGTHOF mesg1)-1, ADDR BytesWritten, 0
        
        
        invoke ExitProcess,0
    main endp
    ;-------------------------------------------------------------------
    
    end main
    

    【讨论】:

      猜你喜欢
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2011-05-09
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      相关资源
      最近更新 更多