Nasm equ $ - 获得错误的长度

时间:2013-06-19 06:13:17

标签: string assembly x86 nasm

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

1 个答案:

答案 0 :(得分: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