2013-06-19 71 views
1

我正在使用nasm構建一個簡單的計算器以顯示迄今爲止學到的什麼... 我正在使用linux。 當我打電話打斷80h時,它會打印出我所有的字符串,而不是指定長度的字符串。我該如何解決這個問題?`

SECTION .data 

     AskForCalculationPrompt: db "Choose which operation you want", 0xA, "1. Addition", 0xA, "2.Subtraction", 0xA, "3. Multiplication", 0xA, "4. Division", 0x3 

     FirstOperandPrompt: db "Enter the first operand:", 0xA 

     SecondOperandPrompt: db "Enter the second operand:", 0xA 

     AnswerPrompt: db "The answer is: " 

     AskForCalculationPromptln: equ $-AskForCalculationPrompt 

     FirstOperandPromptln: equ $-FirstOperandPrompt 

     SecondOperandPromptln: equ $-SecondOperandPrompt 

     AnswerPromptln: equ $-AnswerPrompt 

SECTION .bss 

     Choice: resb 1 
     FirstOperand: resd 1 
     SecondOperand: resd 1 
     Answer: resd 1 

SECTION .text 

     ;Make interrupt to ask for a prompt ask for calculation prompt 
     global _start 

     _start: 

     mov eax, 4 ;Specify sys_write call 
     mov ebx, 1 ;Standard output 
     mov ecx, AskForCalculationPrompt 
     mov edx, AskForCalculationPromptln 
     int 80h 
;error happens here. 
     ;Make interrupt to read textfrom keyboard 

     mov eax, 3 ;Sys_read call 
     mov ebx, 0 ;Standard input file descriptor 0 
     mov ecx, Choice 
     mov edx, 1 
     int 80h 

     ;Determine what we inserted 

     mov al, byte [Choice] 
     cmp al, 0x35 
     je _start` 
+0

這可以幫助你一下:http://www.dreamincode.net/forums/topic/286248-nasm-linux-terminal-inputoutput-wint-80h/ – wazy

回答

2

在NASM,$評估在包含表達式的行開頭的裝配位置。所以,你的代碼做的是將所有字符串的長度等於4個標籤。爲了獲得一個字符串的長度.data

SECTION .data 

    AskForCalculationPrompt: db "Choose which operation you want", 0xA, "1. Addition", 0xA, "2.Subtraction", 0xA, "3. Multiplication", 0xA, "4. Division", 0x3 
    AskForCalculationPromptln: equ $-AskForCalculationPrompt 

    FirstOperandPrompt: db "Enter the first operand:", 0xA 
    FirstOperandPromptln: equ $-FirstOperandPrompt 

    SecondOperandPrompt: db "Enter the second operand:", 0xA 
    SecondOperandPromptln: equ $-SecondOperandPrompt 

    AnswerPrompt: db "The answer is: " 
    AnswerPromptln: equ $-AnswerPrompt 
+0

oh zomg ty非常多 – Kelvin

+0

另外,我有問題的問題,如果我想要做一個cmp反對用戶輸入第二個int80h調用與sys_read,我必須重新加載到eax緩衝區,然後做一個cmp,或者有沒有捷徑? – Kelvin

+0

您可以在不將緩衝區內容加載到註冊表的情況下執行此操作,但必須指定大小。如果你說'al'納斯姆知道這有多大,但你必須告訴Nasm'cmp byte [choice],0x35'。 –