0

When i run this program, it first prints weird symbols and then prints the Hello world. It consists of two files - boot.asm and display.asm. boot.asm is the main files Here are the files- boot.asm

global start
section .text
start:
    mov ax, 07C0h       ; Set up 4K stack space after this bootloader
    add ax, 288     ; (4096 + 512) / 16 bytes per paragraph
    mov ss, ax
    mov sp, 4096

    mov ax, 07C0h       ; Set data segment to where we're loaded
    mov ds, ax
   

    call read_key
    call display

    cmp al, 'm'
    je success
    cmp al, "m"
    jne failure

   
    

success:
    
    jmp success_deal

failure:
    
    jmp failur_deal

success_deal:
    
    ms db " hello world "
    mov si, ms
    mov bl, 2
    call print_string
    %include "display.asm"

    

failur_deal:
    call new_line
    mov al, " "
    call display

read_key:
    push bx
    push cx
    push dx
    push si
    push di
    mov ah, 0x00
    int 0x16
    pop di
    pop si
    pop dx
    pop cx
    pop bx
    ret
display:
    push bx
    push cx
    push dx
    push si
    push di
    movzx si, al
    mov ah, 0Eh
    int 10h
    pop di
    pop si
    pop dx
    pop cx
    pop bx
    ret
new_line:
    push bx
    push cx
    push dx
    push si
    push di
    mov dl, 10
    mov ah, 2
    int 21h
    mov dl, 13
    mov ah, 2
    int 21h
    pop di
    pop si
    pop dx
    pop cx
    pop bx
    ret

display.asm -

print_string:
    mov ah, 0Eh
    cmp bl, 3       ; int 10h 'print char' function
    jne .repeat

.repeat:
    lodsb           ; Get character from string
    cmp al, 0
    je .done        ; If char is zero, end of string
    int 10h         ; Otherwise, print it
    jmp .repeat

.done:
    ret
    times 510-($-$$) db 0   ; Pad remainder of boot sector with 0s
    dw 0xAA55 
    mov bl, 3
    jmp print_string

I get this output - Output

So, how can i remove the symbols and just print the hello world.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
dent
  • 1
  • 1
  • 2
    Don't put data in the execution path. Your `ms` string will currently be executed as code. Moving the `success_deal` label to right after the `ms` declaration whould fix that. – Michael May 03 '22 at 11:59
  • so where should i declare ```ms``` – dent May 03 '22 at 12:14
  • Anywhere that's not in the execution path. The change I suggested should take care of that. – Michael May 03 '22 at 12:18
  • Use a separate label for the declaration, and e.g. put it after a `ret` or after a `jmp`. Or even better put it into a separate section: `.text` ironically is for code. You typically would use `.data` or `rodata`, but the linker file has to support it. Normally you would be using a flat memory model, with every segment being the same, so you would not have to worry further. Otherwise you have to load and specify the segment register (typically `DS` is the default for data access on x86 platform). The segment registers are called selectors in protected mode and have the same basic function. – Sebastian May 03 '22 at 12:32
  • I have declared ms in .data and now it works – dent May 03 '22 at 12:40
  • 2
    @Sebastian: This is a legacy-BIOS bootloader, a 512-byte flat binary. NASM supports separate sections by merely concatenating them all into the binary when you're done. I'm not sure how that works with the `times 510-($-$$) db 0` pad to put `dw 0xAA55` as the last 2 byte of the 512; clearly that needs to be at the actual end of the first 512 bytes, with any bytes you want to access *before* it, not after. (So it's super weird to have `mov bl, 3` / `jmp` after that, unless this code loads another sector from disk.) – Peter Cordes May 03 '22 at 12:53

0 Answers0