装配程序中的错误很少。对于业余爱好者

时间:2013-05-29 20:58:58

标签: assembly

我必须制作一个可以作为计算器使用的程序。我写了这个:

push ds
push 0000H


data segment
    imsg1 db 13,10,'enter 1st number:$'
    imsg2 db 13,10,'enter 2nd number:$'
    imsg3 db 13,10,'sum:$'
    imsg4 db 13,10,'diff:$'
    imsg5 db 13,10,'div:$'
    imsg6 db 13,10,'product:$'
    num dw ?
    num2 dw ?
data ends
   MOV ax,data
   MOV ds,ax
      mov ah,09h
      mov dx,offset imsg1
      int 21h
   mov cx,0
   mov bx,10
   mov num,0            ; Using num instead of dx, as we usually do, because we have to store another number too.

   r1:mov ah,01h        ; For storing first number
      int 21h          
      cmp al,13
      je yy
      sub al,48
      mov cl,al
      mov ax,num
      mul bx
      add ax,cx
      mov num,ax
      jmp r1  

   yy:
      ;to enter 2nd number  
      mov ah,09h
      mov dx,offset imsg2
      int 21h

       mov cx,0
       mov bx,10
       mov num2,0

       r2:mov ah,01h
       int 21h
       cmp al,13
       je yy1
       sub al,48
       mov cl,al
       mov ax,num2
       mul bx
       add ax,cx
       mov num2,ax
       jmp r2


    yy1:
    ;TO PRINT PROMPT MSG FOR SUM
      mov ah,09h
      mov dx,offset imsg3
      int 21h

      mov ax,num                ; Move the numbers to registers before doing any operation
      mov bx,num2               ; Because we will also need the numbers for other operations
      add ax,bx                 ; Adding and storing in ax
      mov cx,0
      mov bx,10
      mov dx,0
    r3:                                 ; To print the sum
       mov dx,0
       div bx
       add dx,48
       push dx
       inc cx
       cmp ax,0
       jg r3
       mov ah,02h
       print:
        pop dx
        int 21h
       loop print

       ;TO PRINT SUBTRACTION PROMPT
       mov ah,09h
      mov dx,offset imsg4
      int 21h
       mov ax,num
       mov bx,num2
       sub ax,bx                ; Subtracting and storing in ax

       mov cx,0
      mov bx,10
      mov dx,0
    r4:                                 ; Printing the subtracted value
       mov dx,0
       div bx
       add dx,48
       push dx
       inc cx
       cmp ax,0
       jg r4
       mov ah,02h
       print1:
        pop dx
        int 21h
       loop print1

       ;TO PRINT DIVISION PROMPT
       mov ah,09h
       mov dx,offset imsg5
       int 21h
       mov dx,0
       mov ax,num
       mov bx,num2
       div bx                   ; Quotient stored in AX

       mov cx,0
       mov bx,10
       mov dx,0
    r5:                                 ; Printing the Quotient
       mov dx,0
       div bx
       add dx,48
       push dx
       inc cx
       cmp ax,0
       jg r5
       mov ah,02h
       print2:
        pop dx
        int 21h
       loop print2

       ;TO PRINT MULTIPLICATION PROMPT
       mov ah,09h
       mov dx,offset imsg6
       int 21h
       mov dx,0
       mov ax,num
       mov bx,num2
       mul bx                   ; Solution in AX

       mov cx,0
       mov bx,10
       mov dx,0
    r6:                                 ; Printing the solution
       mov dx,0
       div bx
       add dx,48
       push dx
       inc cx
       cmp ax,0
       jg r6
       mov ah,02h
       print3:
        pop dx
        int 21h
       loop print3
ret
main endp
code ends
end main

我有错误:

  

第5行(数据段)解析器:预期指令
  第18行(mov dx,offset imsg1):逗号或预期行尾   第39行(mov dx,offset imsg2):逗号或预期行尾   第62行(mov dx,offset imsg3):逗号或预期行结束
  第87行(mov dx,offset imsg4):逗号或预期结束的行   第112行(mov dx,offset imsg5):逗号或预期行尾   第138行(mov dx,offset imsg6):逗号或预期行尾   第162行(主要endp):错误:解析器:指令预期
  第163行(主要目的):错误:解析器:指令预期
  第164行(结束主要):错误:解析器:指令预期

我试图解决这些问题,因为一周没有成功。对不起,我已经在这里推出了完整的代码,但它不是那么长的片段,所以也许有人可以帮我这个.. 谢谢!

2 个答案:

答案 0 :(得分:2)

最简单的事情可能是使用预期的汇编程序......但我曾经做过很多M / Tasm翻译。这是没有问题的!...但是没有抱怨的组装...我的偏好是将它组装到.com文件,但原始代码是用于MZ文件(需要链接器)。

; nasm -f bin -o myprog.com myprog.asm
; or
; nasm -f obj myprog.asm
; link ???

; for -f obj, remove this line
org 100h

segment data
    imsg1 db 13,10,'enter 1st number:$'
    imsg2 db 13,10,'enter 2nd number:$'
    imsg3 db 13,10,'sum:$'
    imsg4 db 13,10,'diff:$'
    imsg5 db 13,10,'div:$'
    imsg6 db 13,10,'product:$'

segment .bss
    num resw 1
    num2 resw 1

segment .text
; if -f obj, put these lines back
;   MOV ax,data
;   MOV ds,ax

      mov ah,09h
      mov dx, imsg1
      int 21h
   mov cx,0
   mov bx,10
   mov word [num],0            ; Using num instead     of dx, as we usually do, because we have to store another number too.

   r1:mov ah,01h        ; For storing first number
      int 21h          
      cmp al,13
      je yy
      sub al,48
      mov cl,al
      mov ax,[num]
      mul bx
      add ax,cx
      mov [num],ax
      jmp r1  

   yy:
      ;to enter 2nd number  
      mov ah,09h
      mov dx, imsg2
      int 21h

       mov cx,0
       mov bx,10
       mov word [num2],0

       r2:mov ah,01h
       int 21h
       cmp al,13
       je yy1
       sub al,48
       mov cl,al
       mov ax,[num2]
       mul bx
       add ax,cx
       mov [num2],ax
       jmp r2


    yy1:
    ;TO PRINT PROMPT MSG FOR SUM
      mov ah,09h
      mov dx, imsg3
      int 21h

      mov ax,[num]              ; Move the numbers to registers before doing any operation
      mov bx,[num2]             ; Because we will also need the numbers for other operations
      add ax,bx                 ; Adding and storing in ax
      mov cx,0
      mov bx,10
      mov dx,0
    r3:                                 ; To print the sum
       mov dx,0
       div bx
       add dx,48
       push dx
       inc cx
       cmp ax,0
       jg r3
       mov ah,02h
       print:
        pop dx
        int 21h
       loop print

       ;TO PRINT SUBTRACTION PROMPT
       mov ah,09h
      mov dx, imsg4
      int 21h
       mov ax,[num]
       mov bx,[num2]
       sub ax,bx                ; Subtracting and storing in ax

       mov cx,0
      mov bx,10
      mov dx,0
    r4:                                 ; Printing the subtracted value
       mov dx,0
       div bx
       add dx,48
       push dx
       inc cx
       cmp ax,0
       jg r4
       mov ah,02h
       print1:
        pop dx
        int 21h
       loop print1

       ;TO PRINT DIVISION PROMPT
       mov ah,09h
       mov dx, imsg5
       int 21h
       mov dx,0
       mov ax,[num]
       mov bx,[num2]
       div bx                   ; Quotient stored in AX

       mov cx,0
       mov bx,10
       mov dx,0
    r5:                                 ; Printing the Quotient
       mov dx,0
       div bx
       add dx,48
       push dx
       inc cx
       cmp ax,0
       jg r5
       mov ah,02h
       print2:
        pop dx
        int 21h
       loop print2

       ;TO PRINT MULTIPLICATION PROMPT
       mov ah,09h
       mov dx, imsg6
       int 21h
       mov dx,0
       mov ax,[num]
       mov bx,[num2]
       mul bx                   ; Solution in AX

       mov cx,0
       mov bx,10
       mov dx,0
    r6:                                 ; Printing the solution
       mov dx,0
       div bx
       add dx,48
       push dx
       inc cx
       cmp ax,0
       jg r6
       mov ah,02h
       print3:
        pop dx
        int 21h
       loop print3
ret

; nasm -f bin -o myprog.com myprog.asm ; or ; nasm -f obj myprog.asm ; link ??? ; for -f obj, remove this line org 100h segment data imsg1 db 13,10,'enter 1st number:$' imsg2 db 13,10,'enter 2nd number:$' imsg3 db 13,10,'sum:$' imsg4 db 13,10,'diff:$' imsg5 db 13,10,'div:$' imsg6 db 13,10,'product:$' segment .bss num resw 1 num2 resw 1 segment .text ; if -f obj, put these lines back ; MOV ax,data ; MOV ds,ax mov ah,09h mov dx, imsg1 int 21h mov cx,0 mov bx,10 mov word [num],0 ; Using num instead of dx, as we usually do, because we have to store another number too. r1:mov ah,01h ; For storing first number int 21h cmp al,13 je yy sub al,48 mov cl,al mov ax,[num] mul bx add ax,cx mov [num],ax jmp r1 yy: ;to enter 2nd number mov ah,09h mov dx, imsg2 int 21h mov cx,0 mov bx,10 mov word [num2],0 r2:mov ah,01h int 21h cmp al,13 je yy1 sub al,48 mov cl,al mov ax,[num2] mul bx add ax,cx mov [num2],ax jmp r2 yy1: ;TO PRINT PROMPT MSG FOR SUM mov ah,09h mov dx, imsg3 int 21h mov ax,[num] ; Move the numbers to registers before doing any operation mov bx,[num2] ; Because we will also need the numbers for other operations add ax,bx ; Adding and storing in ax mov cx,0 mov bx,10 mov dx,0 r3: ; To print the sum mov dx,0 div bx add dx,48 push dx inc cx cmp ax,0 jg r3 mov ah,02h print: pop dx int 21h loop print ;TO PRINT SUBTRACTION PROMPT mov ah,09h mov dx, imsg4 int 21h mov ax,[num] mov bx,[num2] sub ax,bx ; Subtracting and storing in ax mov cx,0 mov bx,10 mov dx,0 r4: ; Printing the subtracted value mov dx,0 div bx add dx,48 push dx inc cx cmp ax,0 jg r4 mov ah,02h print1: pop dx int 21h loop print1 ;TO PRINT DIVISION PROMPT mov ah,09h mov dx, imsg5 int 21h mov dx,0 mov ax,[num] mov bx,[num2] div bx ; Quotient stored in AX mov cx,0 mov bx,10 mov dx,0 r5: ; Printing the Quotient mov dx,0 div bx add dx,48 push dx inc cx cmp ax,0 jg r5 mov ah,02h print2: pop dx int 21h loop print2 ;TO PRINT MULTIPLICATION PROMPT mov ah,09h mov dx, imsg6 int 21h mov dx,0 mov ax,[num] mov bx,[num2] mul bx ; Solution in AX mov cx,0 mov bx,10 mov dx,0 r6: ; Printing the solution mov dx,0 div bx add dx,48 push dx inc cx cmp ax,0 jg r6 mov ah,02h print3: pop dx int 21h loop print3 ret

答案 1 :(得分:2)

在评论部分,您提到要使用NASM汇编代码,但代码是使用TASM / MASM语法编写的。这两种风格之间存在一些重要的差异:

在TASM / MASM语法中:

mov ax,num

将使用 ax中存储的值加载num。在NASM语法中,它将使用地址 ax加载num。要在NASM中获取TASM行为,您需要在地址周围加上括号:

mov ax,[num]

在TASM / MASM语法中:

mov dx,offset imsg1

使用dx的地址加载imsg1(实际上是段内的偏移量)。在NASM语法中,它将简单地变为:

mov dx,imsg1

某些指令(如ENDP)特定于TASM / MASM,并且在NASM中没有真正的对应物。