【问题标题】:Need conversion from MASM to NASM需要从 MASM 转换为 NASM
【发布时间】:2018-02-28 05:07:11
【问题描述】:

附件: 文件 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 \n
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 \n
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 \n
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 \n
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 \n
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 \n
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***

错误被突出显示,线条也被突出显示 感谢任何帮助,我对汇编语言很陌生

【问题讨论】:

  • 您的代码似乎采用 MASM 语法(不是 NASM)。 NASM 无法处理 MASM 语法。您需要使用 MASM(或兼容的工具,如 TASM 甚至 JWASM)来组装此文件。否则,您必须将其转换为 NASM 语法。

标签: assembly x86 dos x86-16


【解决方案1】:
  • 删除程序的前7行并替换为org 0x100
  • .data 中的所有内容移至文件末尾。 COM 文件中不需要的部分。
  • 将所有出现的db ? 更改为db 0
  • 删除第 17 到 22 行 mov ax,@datamov ds,ax 不需要
  • 您使用过的每个地方lea 更改为mov
  • character? 的所有实例更改为[character?]

进行这些更改,您的程序将运行,我使用 DOSBox 0.74 对其进行了测试,输出如下所示;

my name here
Enter first character : R
Enter second character : a
Enter third character : m
R
a
m

这是针对 NASM 修改的程序,并进行了一些个人调整,例如缩短标签

        org 256

        mov     dx, Nme         ;move content to dx

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

        mov     dx, 10      ;print \n
        mov     ah, 2
        int     21h  

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

        mov     dx, prompt1     ;if use lea no need to use offset
        mov     ah, 9       ;print prompt
        int     21h  

        mov     ah, 1       ;ask for chr input
        int     21h
        mov     [chr1] ,al  ; move to labled memory

        mov     dx, 10      ;print \n
        mov     ah, 2
        int     21h  

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

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

        mov     ah, 9       ;print prompt
        int     21h  

        mov     ah,1        ;ask for chr input
        int     21h
        mov     [chr2], al  ; move to labled memory

        mov     dx, 10      ;print \n
        mov     ah, 2
        int     21h  

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

        mov     dx, prompt3     ;if use lea no need to use offset
        mov     ah, 9       ;print prompt
        int     21h

        mov     ah, 1
        int 21h
        mov     [chr3], al   ; move to labled memory 

        mov     dx, 10      ;print \n
        mov     ah, 2
        int     21h  

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

        mov dl, [chr1]      ;move chr value to dl for print
        mov     ah, 2
        int 21h

        mov     dx, 10      ;print \n
        mov     ah, 2
        int     21h  

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

        mov     dl, [chr2]  ;move chr value to dl for print
        mov     ah, 2
        int     21h

        mov     dx, 10      ;print \n
        mov ah, 2
        int     21h  

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

        mov     dl, [chr3]  ;move chr value to dl for print
        mov     ah, 2
        int     21h

        mov ah,4ch
        int 21h

        Nme:        db  'my name here $' ;replace by your name
        prompt1:    db  'Enter  first chr: $' ;ask for chr
        prompt2:    db  'Enter second chr: $' ;ask for chr
        prompt3:    db  'Enter  third chr: $' ;ask for chr

        chr1:   DB 0    ;memory to store chr
        chr2:   DB 0    ;memory to store chr
        chr3:   DB 0    ;memory to store chr

【讨论】:

  • 更改所有字符实例是什么意思?到 [character?] 我到了我唯一的错误是 mov character1-3,al 行上操作码和操作数的无效组合的地步
  • 将问号替换为 1、2 或 3。因此,您需要将 character3 更改为 [character3]。与 MASM 不同,当您想要某个内存位置的内容时,需要将其括在括号中。
  • 当更改为 [character?] 时,我收到错误:无法识别的指令
  • 对不起,我应该澄清一下,?被1-3替换,当我没有[]时,我得到的错误是gyazo.com/3955d24b75863756cd3f56bef5925502,当我使用这个gyazo.com/7cdca36b643a943c66860c23a817c0fc时,我得到了无法识别的指令
猜你喜欢
  • 2021-03-01
  • 1970-01-01
  • 2011-01-03
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
  • 2013-07-05
相关资源
最近更新 更多