2013-05-29 30 views
0

我必須制定一個計算器程序。我寫了這個:裝配程序中幾乎沒有錯誤。對於業餘愛好者

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 

而且我有錯誤:

line 5 (data segment) parser: instruction expected
line 18(mov dx,offset imsg1): comma or end of line expected
line 39(mov dx,offset imsg2): comma or end of line expected
line 62(mov dx,offset imsg3): comma or end of line expected
line 87(mov dx,offset imsg4): comma or end of line expected
line 112(mov dx,offset imsg5): comma or end of line expected
line 138(mov dx,offset imsg6): comma or end of line expected
line 162(main endp): error:parser:instruction expected
line 163(main ends): error:parser:instruction expected
line 164(end main): error:parser:instruction expected

我試圖解決這些問題,因爲沒有成功的一週。對不起,我已經把整個代碼放在這裏,但它不是那麼長的片段,所以也許有人可以幫助我.. 謝謝!

+0

什麼彙編?看起來像Masm/Tasm代碼,錯誤消息看起來像他們可能來自Nasm ... –

+0

只是一個給出未指定語法的猜測,但你可能需要冒號後imsg1:等等,讓他們被識別爲標籤? –

+0

我想在NASM中編譯它。添加冒號沒有奏效。我已經通過輸入segment .data解決了第一個錯誤(數據末尾也有錯誤,但我已經刪除了這一行) –

回答

2

最簡單的事情可能是使用預期的彙編程序......但我曾經做過很多M/Tasm翻譯。這是UNTESTED!...但彙編沒有抱怨...我的首選是將其彙編到.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 

+0

謝謝。我將不得不改進代碼,但現在會更容易:) –

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 

負荷dximsg1地址(實際上其在段內偏移)。在NASM語法,只會變成:

mov dx,imsg1 

一些指令(像ENDP)特定於TASM/MASM,並在NASM沒有真正的對手。

相關問題