Bipul Adh
Bipul Adh

Reputation: 113

NASM prints two strings at a time when i only want one at a time?

I am using nasm to print one string at a time but it prints two string at a time, i have added null character to the end and i am comparing the null character to check for the end of the string but both the strings end up getting printed even when i ask for only one of them.

[org 0x7c00]

mov bx, HELLO_MSG
call print_string

mov bx, GOODBYE_MSG

jmp $

%include "print_string.asm"

;Data
HELLO_MSG:
db 'Hello, World!',0

GOODBYE_MSG:
db 'GOODBYE!',0

times 510-($- $$) db 0
dw 0xaa55

The other file print_string.asm

print_string:
pusha
cld
mov ah,0x0e

 config:    mov al,[bx]
            ;Comparing the strings
            mov cx,[bx]
            cmp cx,0x00 ;Comparing for null
            jne print
je end

print:  int 0x10
        add bx,1
        jmp config

end:    popa
        ret

Upvotes: 0

Views: 208

Answers (1)

Ped7g
Ped7g

Reputation: 16596

; ds:si = asciiz string pointer
print_string:
    pushf            ; store everything to be modified
    push ax
    push bx
    push si
    cld
    mov ah,0x0e
    xor bx,bx        ; bh (page) = 0, bl (color in gfx modes only)
 print_string_loop:
        lodsb
        ; check for null-terminator of string
        test al,al
        jz  print_string_end
        int 0x10
        jmp print_string_loop
 print_string_end:
    pop  si      ; restore everything
    pop  bx
    pop  ax
    popf
    ret

; other code must be adjusted to put pointer into SI, not BX!

This shows common x86 asm idiom to test for zero value.

And I moved the string pointer into si from bx, because then I can use lodsb to load the al (with incrementation of si included), and to avoid bx, as bh and bl are input arguments for int 10h,ah=0Eh. So your old code displays anything sort of by accident, if your string address would load bh with some valid page number which is not visible (by default page 0 is visible in text mode), you wouldn't see your letters on screen.

Plus you surely are not in such hurry, that you can't keep your labels meaningful, and stick with some indentation style (I tried to guess what it is in my answer, not sure if I nailed it). I know it's sort of difficult to keep it tidy when you are just experimenting, but before posting SO question it's probably good to take a step back and clean up as much as possible to get Minimal, Complete, and Verifiable example, make the reading of source as easy as possible to attract the biggest amount of people to try to check for your problems. In the long run cleaning up after your code works will help to save also your time.

Upvotes: 2

Related Questions