2

我正在使用 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`
4

1 回答 1

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
于 2013-06-19T06:24:56.803 回答