Search code examples
assemblynasmdosx86-16dosbox

Moving integers defined in .data section to registers


I'm trying to initialize a memory location with a 16 bit integer and print it.

The result I get is the max value of an unsigned short. The size of val is DW so it's 16 bits, AX is 16 bits as well, so there is no size conflict.

mov [val], word 122 does what I want, but if initialization in the main procedure was my goal, I would use .bss and val2.

Why is this happening and how do I actually get the value stored at val?

I use nasm prog.asm -o prog.exe under DOSBox.

section .data
    val dw 123

section .bss
    val2 resw 1

section .text
    global _start

_start:
    ; mov [val], word 122
    mov ax, word [val]
    mov cx, 0
    mov bx, 10

    loophere:
        mov dx, 0
        div bx                         

        push ax
        add dl, '0'                     

        pop ax                          
        push dx                         
        inc cx                          
        cmp ax, 0                       
        jnz loophere

    mov ah, 2    

    loophere2:
        pop dx                          
        int 21h                         
        loop loophere2




Solution

  • Adding org 0x100 at the beginning of the program solves the problem caused by a bad offset.