Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print strings in assembly language

I am trying to print a string in Q Emulator using NASM. My code is as below:

mov bx,HELLO
mov ah, 0x0e
int 0x10
HELLO:
  db 'Hello', 0
jmp $
times 510-($-$$) db 0
dw 0xaa55

However when I compile this code, the output that I get is

UU

Can anyone please tel me why this is so? And how to get the required string as output?

Thanks in advance.

like image 357
Sanket Gupte Avatar asked Oct 29 '22 18:10

Sanket Gupte


1 Answers

ALRIGHT SO here is a thingamabob to your question

In order to load the string, you must move it into si (don't really want to go to deep but just do it). Next in order to load a character into the register AL use lodsb. Next, we must print it so use int 10h mov ah, 0Eh. Int 10h handles the video and ah tells BIOS to print whatever we have in al (aka lodsb). Next, we must have an ending loading char so we just don't loop forever. Me personally I use 0x00 however you use 0. 0x00 is much better in my case because not only can u use 0, 0x00 does not print anything so why would u ever need it?

ALRIGHT so we got everything done and going here is the code:

    mov si, message       ;The message location *you can change this*
    call print            ;CALL tells the pc to jump back here when done

    print:
      mov ah, 0Eh         ;Set function

    .run:
      lodsb               ;Get the char
    ; cmp al, 0x00        ;I would use this but ya know u dont so use:
      cmp al, 0           ;0 has a HEX code of 0x48 so its not 0x00
      je .done            ;Jump to done if ending code is found
      int 10h             ;Else print
      jmp .run            ; and jump back to .run

    .done:
      ret                 ;Return

    message           db  'Hello, world', 0 ;IF you use 0x00
    ;message          db  'Hello, world', 0x00

like image 50
Logan Rios Avatar answered Nov 15 '22 12:11

Logan Rios