需要从MASM转换到NASM

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了需要从MASM转换到NASM相关的知识,希望对你有一定的参考价值。

附加文件:文件dosbox_003.png(5.722 KB)编写一个NASM,它将:

在一行显示您的姓名。 (使用int 21h,函数9.回车和换行的字符分别为0Dh和0Ah。)在下一行,显示提示(再次使用int 21h,function9)并从键盘读取三个字符(使用int 21h,fcn 1.)将字符存储在适当标记的字节变量中。打印三个字符,每行一个(根据您的实现方式,使用int 21h,函数9或函数2,您的选择。)使用NASM组装程序并使用DOSBox执行它。

用命令提示符组装

鉴于此代码(错误加粗和突出显示行)我有这些错误

C:NASM>nasm bal-lab2.asm -o bal-lab2.com
bal-lab2.asm:5: error: attempt to define a local label before any non-local 
labels
bal-lab2.asm:5: error: parser: instruction expected
bal-lab2.asm:6: error: attempt to define a local label before any non-local 
labels
bal-lab2.asm:6: error: parser: instruction expected
bal-lab2.asm:7: warning: label alone on a line without a colon might be in 
error [-w+orphan-labels]
bal-lab2.asm:7: error: attempt to define a local label before any non-local 
labels
bal-lab2.asm:17: warning: label alone on a line without a colon might be in 
error [-w+orphan-labels]
bal-lab2.asm:18: error: parser: instruction expected
bal-lab2.asm:127: error: symbol `main' redefined
bal-lab2.asm:127: error: parser: instruction expected
bal-lab2.asm:128: error: parser: instruction expected

用这个代码

;ff
;ff
;ff

.model small line ***LINE 5***
.stack 100h ***LINE 6***
.data ***LINE 7***  
nameString db 'my name here $' ;replace by your name
prompt1 db 'Enter first character : $' ;ask for character
prompt2 db 'Enter second character : $' ;ask for character
prompt3 db 'Enter third character : $' ;ask for character

character1 DB ? ;memory to store character
character2 DB ? ;memory to store character
character3 DB ? ;memory to store character

.code ***LINE 17**
main proc ***LINE 18***


mov ax,@data ;move data address to ax
mov ds,ax ;move ax to data segment  


lea dx , nameString ;move content to dx

mov ah,9 ;ask to print array of string
int 21h

mov dx,10 ;print 

mov ah,2
int 21h  

mov dx,13 ;cursor at first position
mov ah,2
int 21h

lea dx , prompt1 ;if use lea no need to use offset

mov ah,9 ;print prompt
int 21h  
;ask for character input
mov ah,1
int 21h

mov character1,al ; move to labled memory


mov dx,10 ;print 

mov ah,2
int 21h  

mov dx,13 ;cursor at first position
mov ah,2
int 21h

lea dx , prompt2 ;if use lea no need to use offset

mov ah,9 ;print prompt
int 21h  
;ask for character input
mov ah,1
int 21h

mov character2,al ; move to labled memory


mov dx,10 ;print 

mov ah,2
int 21h  

mov dx,13 ;cursor at first position
mov ah,2
int 21h

lea dx , prompt3 ;if use lea no need to use offset

mov ah,9 ;print prompt
int 21h  
;ask for character input
mov ah,1
int 21h

mov character3,al ; move to labled memory  


mov dx,10 ;print 

mov ah,2
int 21h  

mov dx,13 ;cursor at first position
mov ah,2
int 21h

mov dl,character1 ;move character value to dl for print
mov ah,2
int 21h

mov dx,10 ;print 

mov ah,2
int 21h  

mov dx,13 ;cursor at first position
mov ah,2
int 21h

mov dl,character2 ;move character value to dl for print
mov ah,2
int 21h


mov dx,10 ;print 

mov ah,2
int 21h  

mov dx,13 ;cursor at first position
mov ah,2
int 21h

mov dl,character3 ;move character value to dl for print
mov ah,2
int 21h


mov ah,4ch
int 21h
main endp ***LINE 127***
end main ***LINE 128***

错误突出显示,并且线条太过感激任何帮助,我对汇编语言非常新

答案
  • 删除前7行程序并替换为org 0x100
  • .data中的所有内容移动到文件末尾。 COM文件中不需要的部分。
  • 将所有出现的qazxsw poi更改为qazxsw poi
  • 删除第17到22行mov ax,@ data和mov ds,不需要ax
  • 你使用db ?的每个地方都改为db 0
  • lea的所有实例更改为mov

进行这些更改,你的程序将工作,我使用DOSBox 0.74进行测试,输出看起来像这样;

character?

这是您为NASM修改的程序,以及一些个人调整,如缩短标签

[character?]

以上是关于需要从MASM转换到NASM的主要内容,如果未能解决你的问题,请参考以下文章

ASM:MASM、NASM、FASM?

masm - 字母大小写转换

masm与nasm的搭建使用

汇编学习笔记-NASM环境搭建(nasm with vs2017)

编写自定义PE结构的程序(如何手写一个PE,高级编译器都是编译好的PE头部,例如MASM,TASM等,NASM,FASM是低级编译器.可以自定义结构)

如何将 SSE 汇编代码转换为 AVX1/2 汇编代码?