装配:阅读装配说明行

发布于 2024-12-10 08:52:16 字数 1708 浏览 0 评论 0原文

我正在开发一个程序,该程序从包含汇编指令的文件中读取输入,然后输出每行是否包含标签、操作码、oper1、oper2 或注释。这是我到目前为止所得到的:

        .model  small
        .8086

        .data
line        db      'LABEL=','$'
opcode      db      'OPCODE=','$'
oper1       db      'OPER1=','$'
oper2       db      'OPER2=','$'
com         db      'COMMENT=',13,10,13,10,'$'
filemsg     db      '... end of file',13,10,1Ah,'$'

        .code

start:      
        mov     ax,@data
        mov     ds,ax

progloop:
        mov     ah,8
        int     21h

        cmp     al,1Ah
        je      eof

        mov     dl,al
        mov     ah,2
        int     21h

        cmp     dl,3Ah    ; this is where I would check for a colon. incomplete for now

        cmp     dl,0Ah
        je      eol

        jmp     progloop

eol:
        mov     dx,offset line
        mov     ah,9
        int     21h
        mov     dx,offset opcode
        mov     ah,9
        int     21h
        mov     dx,offset oper1
        mov     ah,9
        int     21h
        mov     dx,offset oper2
        mov     ah,9
        int     21h
        mov     dx,offset com
        mov     ah,9
        int     21h

        jmp     progloop

eof:       
        mov     dx,offset filemsg
        mov     ah,9
        int     21h

exit:       mov     ax,4c00h
        int     21h
        end     start

该程序基本上应该像这样输出:

Addval:   add   [salary],1000   ; this line has all five operands
LABEL=Y   OPCODE=Y   OPER1=Y   OPER2=Y   COMMENT=Y

testit:                         ; a label and a comment
LABEL=Y   OPCODE=N   OPER1=N   OPER2=N   COMMENT=Y

我不知道如何正确地处理这个问题。我应该创建一个 linemsg 并让它跟踪 LABEL=、OPCODE= 等吗?我应该如何跟踪 Y/N 标志?

I am working on a program that is to read in input from a file that includes assembly instructions and then output whether each line includes a label, opcode, oper1, oper2, or comment. Here is what I have so far:

        .model  small
        .8086

        .data
line        db      'LABEL=','

The program is basically supposed to output like so:

Addval:   add   [salary],1000   ; this line has all five operands
LABEL=Y   OPCODE=Y   OPER1=Y   OPER2=Y   COMMENT=Y

testit:                         ; a label and a comment
LABEL=Y   OPCODE=N   OPER1=N   OPER2=N   COMMENT=Y

I am not sure how to properly go about this. Should I make a linemsg and have it keep track of LABEL=, OPCODE=, etc? How should I keep track of the Y/N flags?

opcode db 'OPCODE=','

The program is basically supposed to output like so:


I am not sure how to properly go about this. Should I make a linemsg and have it keep track of LABEL=, OPCODE=, etc? How should I keep track of the Y/N flags?

oper1 db 'OPER1=','

The program is basically supposed to output like so:


I am not sure how to properly go about this. Should I make a linemsg and have it keep track of LABEL=, OPCODE=, etc? How should I keep track of the Y/N flags?

oper2 db 'OPER2=','

The program is basically supposed to output like so:


I am not sure how to properly go about this. Should I make a linemsg and have it keep track of LABEL=, OPCODE=, etc? How should I keep track of the Y/N flags?

com db 'COMMENT=',13,10,13,10,'

The program is basically supposed to output like so:


I am not sure how to properly go about this. Should I make a linemsg and have it keep track of LABEL=, OPCODE=, etc? How should I keep track of the Y/N flags?

filemsg db '... end of file',13,10,1Ah,'

The program is basically supposed to output like so:


I am not sure how to properly go about this. Should I make a linemsg and have it keep track of LABEL=, OPCODE=, etc? How should I keep track of the Y/N flags?

.code start: mov ax,@data mov ds,ax progloop: mov ah,8 int 21h cmp al,1Ah je eof mov dl,al mov ah,2 int 21h cmp dl,3Ah ; this is where I would check for a colon. incomplete for now cmp dl,0Ah je eol jmp progloop eol: mov dx,offset line mov ah,9 int 21h mov dx,offset opcode mov ah,9 int 21h mov dx,offset oper1 mov ah,9 int 21h mov dx,offset oper2 mov ah,9 int 21h mov dx,offset com mov ah,9 int 21h jmp progloop eof: mov dx,offset filemsg mov ah,9 int 21h exit: mov ax,4c00h int 21h end start

The program is basically supposed to output like so:

I am not sure how to properly go about this. Should I make a linemsg and have it keep track of LABEL=, OPCODE=, etc? How should I keep track of the Y/N flags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-12-17 08:52:16

最简单但并不完美的是这样的...

首先确定一行上是否有注释,然后将其从那里删除(物理上或将有效行长度缩短到第一个分号的位置)。

然后看看剩下的部分是否有冒号。如果有,其左侧是标签名称。您可以删除它(或者再次跳过它,假装该行在冒号之后开始)。

如果还剩下什么,第一项就是操作码。如果后面有任何内容,那就是操作数(一个或多个,用逗号分隔)。

这不是一个完美的解决方案,因为在各种 x86 汇编器中都支持许多更复杂的构造,例如,如果指定了段,则会有一个与标签无关的冒号:

mov al, byte ptr es:[bx]

在上面的 mov al, byte ptr es 不是标签。

或者您可能有一个如下所示的数组声明。数组的名称不是操作码,它实际上是一个标签,但后面没有冒号:

MyArray db 1,2,3,4,5 ; array of 5 bytes

并且您还可能会在字符和字符串文字内部遇到标点符号,它们不会将行分隔为标签、操作数和注释:

MyString db ':,a;'

这里, MyString db ' 不是一个标签,只是因为它后面有一个冒号。 a 并不是一个操作数,只是因为它前面有一个逗号。最后, ;' 并不是一个注释,因为里面有一个分号。

为了完全支持所有这些可能性,您需要实现一个更复杂的解决方案,可能涉及解析状态机。

The simplest, but not perfect, would be something like this...

First determine if there's a comment on a line and then remove it from there (either physically or shorten the effective line length to the position of the first semicolon).

Then see if there's a colon in what remains. If there is, to the left of it is a label name. You can remove it (or again, just skip past it, pretending that the line begins after the colon).

If there's still anything left, the first item is an opcode. If there's anything after it, it's the operands (one or multiple separated with commas).

This is not a perfect solution because in various x86 assemblers there are a number of more complex constructs supported, for example, if a segment is specified, there will be a colon that has nothing to do with labels:

mov al, byte ptr es:[bx]

In the above mov al, byte ptr es is not a label.

Or you may have an array declared like in the below. The array's name isn't an opcode, it's effectively a label, but there's no colon after it:

MyArray db 1,2,3,4,5 ; array of 5 bytes

And you may also encounter punctuators inside of character and string literals that do not separate the line into labels, operands and comments:

MyString db ':,a;'

Here, MyString db ' isn't a label just because there's a colon after it. a isn't an operand just because there's a comma right before it. And, finally, ;' isn't a comment just because there's a semicolon in there.

To fully support all these possibilities you'd need to implement a more complex solution, likely involving a parsing state machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文