Why does my program print garbage?

1

I have the following code:

[bits 16]

segment .text
    global start

start:
    lea si,[msg]
    call caracter

    mov ah,4Ch ; end the program
    int 21

caracter:
    mov al,[si]
    cmp al,0
    jz endprint

    inc si
    mov ah,0Eh
    int 10h
    jmp caracter

endprint:
    ret

segment .data
    msg db 'Hola mundo',0

It is supposed to print the msg message character by character until it meets a 0, the problem is that it only prints trash, what do I do wrong?

    
asked by Cristofer Fuentes 20.05.2017 в 11:18
source

1 answer

1

After further investigation, the problem was really simple to solve, I just needed to add 1 line at the beginning of the document org 0x100 would look like this:

[bits 16]
org  0x100 ; aqui la linea que faltaba para que funcionara

segment .text
    global start

start:
    lea si,[msg]
    call caracter

    mov ah,4Ch ; end the program
    int 21     ; hello darkness my old friend

caracter:
    mov al,[si]
    cmp al,0
    jz endprint

    mov ah,0Eh ; imprime el caracter
    int 10h
    inc si     ; avanza al siguiente caracter
    jmp caracter



endprint:
    ret

segment .data
    msg db "Hola mundo",0

This is necessary because according to Nasm's official documentation:

  

In this model, the code you end up writing starts at 0x100 ...

Which means that we started working in memory address 0x100 and therefore it is necessary to adjust the location counter to that position which is done with org 0x100 , without this line it was evident that garbage was printed that the information was not in the direction one thought.

    
answered by 21.05.2017 / 00:21
source