代码之家  ›  专栏  ›  技术社区  ›  Luke Blevins

x86英特尔汇编程序仅在NASM中编译

  •  2
  • Luke Blevins  · 技术社区  · 7 年前

    http://www.nasm.us/doc/nasmdoc2.html#section-2.2 Windows是否要求以与基于Unix的操作系统不同的方式调用中断?

    最后,我需要知道是否有更有效的方法来实现相同的结果。

    HelloWorld组装程序:

    section .data                              ;Constant Data Section
        userMsg db 'What is your name?'        ;Request Name Input
        lengthMsg equ $-userMsg                ;Set length of request
    
        returnMsg db 'Hello there, '           ;Return Message
        lengthRet equ $-returnMsg              ;Set length of returned message
    
    section .bss
        number resb 5
    
    section .text
        global _start
    
    
    _start:
        mov eax, 4                             ;Print first message to screen 
        mov ebx, 1
        mov ecx, userMsg
        mov edx, lengthMsg
        int 80h
    
        mov eax, 3
        mov ebx, 2
        mov ecx, number
        mov edx, 5
        int 80h
    
        mov eax, 4
        mov ebx, 1
        mov ecx, returnMsg
        mov edx, lengthRet
        int 80h
    
        mov eax, 4
        mov ebx, 1
        mov ecx, number
        mov edx, 5
        int 80h
    
        mov eax, 1
        mov ebx, 0
        int 80h
    

    这些是组装文件时显示的错误。

    Microsoft (R) Macro Assembler Version 6.14.8444
    Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.
    
     Assembling: C:\Projects\theapp.asm
    C:\Projects\theapp.asm(1) : error A2008: syntax error : section
    C:\Projects\theapp.asm(2) : error A2034: must be in segment block
    C:\Projects\theapp.asm(3) : error A2034: must be in segment block
    C:\Projects\theapp.asm(5) : error A2034: must be in segment block
    C:\Projects\theapp.asm(6) : error A2034: must be in segment block
    C:\Projects\theapp.asm(8) : error A2008: syntax error : section
    C:\Projects\theapp.asm(9) : error A2008: syntax error : number
    C:\Projects\theapp.asm(11) : error A2008: syntax error : section
    C:\Projects\theapp.asm(12) : error A2008: syntax error : global
    C:\Projects\theapp.asm(15) : error A2034: must be in segment block
    C:\Projects\theapp.asm(16) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(17) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(18) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(19) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(20) : error A2034: must be in segment block
    C:\Projects\theapp.asm(22) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(23) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(24) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(25) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(26) : error A2034: must be in segment block
    C:\Projects\theapp.asm(28) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(29) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(30) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(31) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(32) : error A2034: must be in segment block
    C:\Projects\theapp.asm(34) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(35) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(36) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(37) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(38) : error A2034: must be in segment block
    C:\Projects\theapp.asm(40) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(41) : error A2085: instruction or register not accepted in current CPU mode
    C:\Projects\theapp.asm(42) : error A2034: must be in segment block
    C:\Projects\theapp.asm(45) : error A2088: END directive required at end of file
    _
    Assembly Error
    Press any key to continue . . .
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Cody Gray    7 年前

    NASM ,它使用的语法与MASM(微软的汇编程序)略有不同。这就是为什么会出现语法错误。例如,虽然NASM section .data section .text ,MASM .data .code

    您的代码也是为Linux编写的。您可以判断,因为它正在通过调用中断80h进行系统调用( int 80h

    mov eax, 1
    mov ebx, 0
    int 80h
    

    退出流程。在Windows上,您可以调用 ExitProcess API函数,该函数由操作系统附带的kernel32.dll库导出。这些操作系统API函数的原型随 Windows SDK 在C标题中( Windows.h ). 您可以使用以下工具 h2inc.exe MASM32 libraries ,他们为你做了这些(还有更多)。

    然而,请注意,如果您真的想用汇编语言编写真正的程序,则只需要担心所有这些特定于操作系统的东西。如果你只是想 汇编语言是如何工作的,那么这就是不必要的复杂性。在汇编语言中执行的基本算术和位运算在所有操作系统上都是一样的,因此这是您应该重点学习的内容。为了避免很多挫折,请确保您的教程/书籍与汇编程序相匹配

    int ). 因此,无论如何,你最终都会为Windows大量重写代码,这对你学习除Windows编程之外的任何东西都没有帮助。如果你想学习Windows编程,那么从汇编语言中学习是毫无意义的。从C开始执行,如下所示 the classic book (5th edition only ) 。所有API调用都是