简体   繁体   English

为什么我的汇编程序不起作用?

[英]Why is my assembly program not working?

I'm completely new to Assembly and right now I'm using X86 assembly. 我对Assembly完全陌生,现在我正在使用X86 Assembly。 I'm using the NASM and right now my code isn't compiling. 我正在使用NASM,现在我的代码尚未编译。 I got it from a book and basically the code works with strings: 我是从书中获得的,基本上,代码可用于字符串:

; This program demonstrates the string-handling procedures in
; the book's link library.
INCLUDE Irvine32.inc
.data
string_1 BYTE "abcde////",0
string_2 BYTE "ABCDE",0
msg0 BYTE "string_1 in upper case: ",0
msg1 BYTE "string1 and string2 are equal",0
msg2 BYTE "string_1 is less than string_2",0
msg3 BYTE "string_2 is less than string_1",0
msg4 BYTE "Length of string_2 is ",0
msg5 BYTE "string_1 after trimming: ",0
.code
main PROC
call trim_string
call upper_case
call compare_strings
call print_length
exit
main ENDP
trim_string PROC
; Remove trailing characters from string_1.
INVOKE Str_trim, ADDR string_1, '/'
mov edx,OFFSET msg5
call WriteString
mov edx,OFFSET string_1
call WriteString
call Crlf
ret
trim_string ENDP
upper_case PROC
; Convert string_1 to upper case.
mov edx,OFFSET msg0
call WriteString
INVOKE Str_ucase, ADDR string_1
mov edx,OFFSET string_1
call WriteString
call Crlf
ret
upper_case ENDP
compare_strings PROC
; Compare string_1 to string_2.
INVOKE Str_compare, ADDR string_1, ADDR string_2
.IF ZERO?
mov edx,OFFSET msg1
.ELSEIF CARRY?
mov edx,OFFSET msg2 ; string 1 is less than...
.ELSE
mov edx,OFFSET msg3 ; string 2 is less than...
.ENDIF
call WriteString
call Crlf
ret
compare_strings ENDP
print_length PROC
; Display the length of string_2.
mov edx,OFFSET msg4
call WriteString
INVOKE Str_length, ADDR string_2
call WriteDec
call Crlf
ret
print_leng

th ENDP
END main

Like I said I'm using NASM so this might be the problem but it should still work, but when i compile it using nasm -f win32 other.asm -o other.o it comes up witha plethora of errors most of which say parser instruction expected. 就像我说的那样,我正在使用NASM,所以这可能是问题所在,但它仍然可以正常工作,但是当我使用nasm -f win32 other.asm -o other.o对其进行编译时,会出现很多错误,其中大多数是解析器错误预期的指示。 I'm using a Windows 8 64 bit, but there's no reason why it can't run a 32 bit program- correct me if i'm wrong. 我使用的是Windows 8 64位,但是没有理由为什么它不能运行32位程序-如果我输入错误,请纠正我。 The problem with the MASM compiler is that it says I need precisely Visual C++ Express 2005 (PRECISELY 2005) to download otherwise it doesn't download. MASM编译器的问题在于,它说我恰好需要Visual C ++ Express 2005(准确地说是2005)才能下载,否则就不会下载。 How can I get this program to work along with other programs I might write in future- and I did remember to put the nasm assembler into the bin file of my C compiler. 我如何使该程序与将来可能要编写的其他程序一起使用,我确实记得将nasm汇编器放入C编译器的bin文件中。 Like I said I'm fairly new and believe it or not the book doesn't actually tell you how to run the program. 就像我说的那样,我很新,不管信不信,这本书都没有真正告诉您如何运行程序。 Also is there a way to download masm without VS 2005 (which i can't seem to find anyway) or any of the VS for that metter 也有一种方法可以下载没有VS 2005(无论如何我似乎都找不到)或任何适用于该流量计的VS的Masm

Other programs (in ASM) don't seem to run on it either. 其他程序(在ASM中)似乎也不在其上运行。 I'm pretty sure this is the windows version otherwise it wouldn't have downloaded to begin with. 我很确定这是Windows版本,否则一开始就不会下载。

Why is my assembly program not working? 为什么我的汇编程序不起作用?

Because you're trying to compile assembler code in MASM syntax with NASM. 因为您正在尝试使用NASM以MASM语法编译汇编代码。

Option 1: get MASM 选项1:获取MASM
Do not try to put MASM code into NASM. 不要尝试将MASM代码放入NASM。
This will not work because every assembler has its own syntax. 这将不起作用,因为每个汇编程序都有自己的语法。
(Yes, I agree that's messed up) . (是的,我同意这很糟)

As per @Frank's suggestion download masm from: http://www.masm32.com/masmdl.htm 按照@Frank的建议,从以下位置下载masm: http//www.masm32.com/masmdl.htm
Note that the SDK (software development kit) is the actual thing with all the tools needed to compile code. 请注意,SDK(软件开发工具包)是编译代码所需的所有工具的真实内容。
The masm installer rebuilds the development tools by recompiling them. masm安装程序通过重新编译来重建开发工具。 This is somewhat unusual but it does ensure that all the tools needed to compile your code are present and working. 这有点不寻常,但是它可以确保编译代码所需的所有工具都可以使用。

Option 2: use NASM source code examples 选项2:使用NASM源代码示例
See: https://www.google.co.za/search?q=sample+nasm+programs&ie=utf-8&oe=utf-8&rls=org.mozilla:nl:official&client=firefox-a&gws_rd=cr&ei=uPNgUp-wBIqihgf45oDwCQ 请参阅: https : //www.google.co.za/search?q=sample+nasm+programs&ie=utf-8&oe=utf-8&rls=org.mozilla : nl : official&client=firefox-a&gws_rd=cr&ei=uPNgUp-wBIqihgf45oDwCQ

Option 3: learn the differences between MASM and NASM 选项3:了解MASM和NASM之间的区别
The nasm manual has a section on the differences with masm : http://www.nasm.us/doc/nasmdoc2.html#section-2.2 nasm手册中有一节介绍了与masm的区别: http : //www.nasm.us/doc/nasmdoc2.html#section-2.2
This might also be helpful: http://left404.com/2011/01/04/converting-x86-assembly-from-masm-to-nasm-3/ 这可能也有帮助: http : //left404.com/2011/01/04/converting-x86-assembly-from-masm-to-nasm-3/

Option 4: get an auto-translator 选项4:获取自动翻译器
Lucky for you there are auto-translators which will transform MASM code into NASM. 幸运的是,有一些自动翻译器可以将MASM代码转换为NASM。
Here's one: http://www.devoresoftware.com/nomyso/ 这是一个: http : //www.devoresoftware.com/nomyso/
Note that this particular one requires perl . 请注意,这一特定功能需要perl

Use the following code: 使用以下代码:

; String Library Demo   (StringDemo.asm)

; This program demonstrates the string-handling procedures in 
; the book's link library.

INCLUDE Irvine32.inc

.data
string_1 BYTE "abcde////",0
string_2 BYTE "ABCDE",0
msg0     BYTE "string_1 in upper case: ",0
msg1     BYTE "string1 and string2 are equal",0
msg2     BYTE "string_1 is less than string_2",0
msg3     BYTE "string_2 is less than string_1",0
msg4     BYTE "Length of string_2 is ",0
msg5     BYTE "string_1 after trimming: ",0

.code
main PROC

    call    trim_string
    call    upper_case
    call    compare_strings
    call    print_length

    exit
main ENDP

trim_string PROC
; Remove trailing characters from string_1.

    INVOKE Str_trim, ADDR string_1,'/'
    mov     edx,OFFSET msg5
    call    WriteString
    mov     edx,OFFSET string_1
    call    WriteString
    call    Crlf

    ret
trim_string ENDP

upper_case PROC
; Convert string_1 to upper case.

    mov     edx,OFFSET msg0
    call    WriteString
    INVOKE  Str_ucase, ADDR string_1
    mov     edx,OFFSET string_1
    call    WriteString
    call    Crlf

    ret
upper_case ENDP

compare_strings PROC
; Compare string_1 to string_2.

    INVOKE Str_compare, ADDR string_1, ADDR string_2
    .IF ZERO?
    mov edx,OFFSET msg1
    .ELSEIF CARRY?
    mov edx,OFFSET msg2     ; string 1 is less than...
    .ELSE
    mov edx,OFFSET msg3     ; string 2 is less than...
    .ENDIF
    call    WriteString
    call    Crlf

    ret
compare_strings  ENDP

print_length PROC
; Display the length of string_2.

    mov     edx,OFFSET msg4
    call    WriteString
    INVOKE  Str_length, ADDR string_2
    call    WriteDec
    call    Crlf

    ret
print_length ENDP

END main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM