-1

Related, but not helpful to my current situation: nasm dos interrupt (output string)

(I just wanted to clarify this is not a duplicate)

What I am trying to do is create a prompt that will say "Enter a Base 10 number: " to the user. After that, I will convert that number to binary, octal, and hexadecimal. I'm coming across an issue, however, and I'm sure it's very simple but I've been staring at this code way too long to understand what's wrong.

What happens is it outputs the "Enter a Base Ten Number: ", then blinks about 3 times and automatically shuts down my DOSbox emulator. Any ideas?

Here is the code:

org   0x100               

mov   bx, 1                  ;init bx to 1
mov   ax, 0100h

mov   dx, msg                ;message's address in dx
mov   cx, len
mov   ah, 0x40
int   0x21

msg   db 'Enter a Base Ten Number: '
len   equ $ -msg

;all of the code above runs fine it seems. It gets to this point
;but does not then run the following code

mov   ah, 9
int   21h

while:
        cmp   ax, 13        ;is char = carriage return?
        je    endwhile      ;if so, we're done
        shl   bx, 1         ;multiplies bx by 2
        and   al, 01h       ;convert character to binary
        or    bl, al        ;"add" the low bit
        int   21h
        jmp   while
endwhile
Community
  • 1
  • 1
Jud
  • 1,324
  • 3
  • 24
  • 47

3 Answers3

2
int   0x21

msg   db 'Enter a Base Ten Number: '  <- OOPS. This will be executed as code.
len   equ $ -msg

;all of the code above runs fine it seems. It gets to this point
;but does not then run the following code

mov   ah, 9

You've placed data in the code path. There's no way for the CPU to know that what you've stored at msg isn't a bunch of instructions. Either do a jmp after the int 0x21 that jumps to a label after the data, or place your data after all the code in your program.

Michael
  • 57,169
  • 9
  • 80
  • 125
1

You have placed msg in the text segment, which contains code. As such, when it is executed, things you didn't intend happen.

Place msg in the appropriate section for initialised data, .data, with:

section .data
    msg:  db 'Enter a Base Ten Number: '
    len   equ $ -msg

section .text
start:
    mov   bx, 1
    # etc.

See the NASM documentation on how to produce COM files.

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
0

In your data section, set up what you want the prompt to be. In this example, I'll use ">>" to be the prompt:

section .data
    prompt   db   ">>", 13, 10, '$'

Then in your bss section, set up a string destination, and reserve a certain number of bytes for the user to use. In this example, I'll use string1 to be the destination, and I'll reserve 80 bytes:

section.bss
    string1   resb   80

From there, just do the logic in your text section as such:

    section .text
       mov dx, prompt ; move your prompt into your data register
       mov ah, 9      ; use the print string function
       int 21h        ; print your prompt

       mov     cx, 0  ; set your counter register to 0
       cld            ; clear direction to process string left to right
       mov     di, string1       ;set string1 as destination
       mov     ah, 1             ;read char fcn
       int     21h               ;read it
Jud
  • 1,324
  • 3
  • 24
  • 47