2

I am using nasm to build a simple calculator to show what ive learned so far... I am using linux. When i make call to interrupt 80h it prints out all my strings, not the one of specified length. How do I fix this?`

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`
Kelvin
  • 233
  • 1
  • 4
  • 13
  • This might help you a bit: http://www.dreamincode.net/forums/topic/286248-nasm-linux-terminal-inputoutput-wint-80h/ – wazy Jun 19 '13 at 06:19

1 Answers1

3

In NASM, $ evaluates to the assembly position at the beginning of the line containing the expression. So, what your code does is equate the length of all strings to 4 labels. To obtain the length of a string in .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
Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
  • Also i have on emore question, if i want to do a cmp against what the user inputted in the second int80h call with sys_read, do i have to reload the buffer into eax and then do a cmp, or is there a shortcut? – Kelvin Jun 19 '13 at 06:36
  • You can do it without loading buffer content into a register, but you'll have to specify size. If you say `al` Nasm knows how big that is, but you will have to tell Nasm `cmp byte [choice], 0x35`. – Frank Kotler Jun 19 '13 at 07:04