NASM - The Netwide Assembler

NASM Forum => Example Code => Topic started by: Structure on December 11, 2019, 04:03:35 PM

Title: Hello World
Post by: Structure on December 11, 2019, 04:03:35 PM
.zeroTerminated:
Code: [Select]
times 512-($-$$) db 0
jmp start

print:
 pusha
 .printloop:
   mov al, [si]
   cmp al, 0
   jne .printchar
   popa
   ret
 .printchar:
   mov ah, 0x0e
   int 0x10
   add si, 1
   jmp .printloop
ret

string: db "Hello World.", 0

start:
  mov si, string
  call print
Title: Re: Hello World
Post by: debs3759 on December 11, 2019, 09:42:16 PM
.zeroTerminated:
Code: [Select]
times 512-($-$$) db 0
jmp start

print:
 pusha
 .printloop:
   mov al, [si]
   cmp al, 0
   jne .printchar
   popa
   ret
 .printchar:
   mov ah, 0x0e
   int 0x10
   add si, 1
   jmp .printloop
ret

string: db "Hello World.", 0

start:
  mov si, string
  call print

In your code, if that is the entire code, $ = $$ (both are at the start of your code). So

times 512-($-$$) db 0

inserts 512 0 bytes at the start of the code. What is that for?
Title: Re: Hello World
Post by: Structure on December 11, 2019, 10:06:25 PM
The first 512 kb are reserved for the boot sector:

https://en.wikipedia.org/wiki/Boot_sector

or a file header, etc...
Title: Re: Hello World
Post by: debs3759 on December 11, 2019, 11:13:17 PM
I did wonder if this was related to a boot sector, but it wasn't obvious from your code. My FAT12 boot sector code is lower down in this forum :)
Title: Re: Hello World
Post by: T145 on December 13, 2019, 04:42:44 AM
You don't need any of that bootcode, since you're not making anything to boot into, like a virtual floppy disk. As you know, in 16 bit land we just start executing from the top, so you can always just use the following macro to print your string:

Code: [Select]
%macro puts 1
    pushaw
    mov     si, %1
    mov     ah, 0Eh    ; INT 10h doesn't trash ah or bx
    mov     bx, 7      ; video mode support
%%read:
    lodsb
    test    al, al     ; check for null-terminating character (0)
    jz      %%done
    int     10h        ; print character in al
    jmp     %%read
%%done:
    popaw
%endmacro

Then just `puts string` and either `ret` or `jmp $` to avoid running into the data section.
Title: Re: Hello World
Post by: Structure on January 02, 2020, 05:21:42 PM
rebuild:

Code: [Select]
string: db "Hello World", 0
%macro print 1
    mov si, %1
    %%char_loop:
        mov cl, [si]
        inc si
        cmp cl, 0 
        je  %%exit_loop
        mov al, cl
        mov ah, 0x0e
        int 0x10
    call %%char_loop
  %%exit_loop:
%endmacro
print string
jmp $

 8)
Title: Re: Hello World
Post by: michaellongge on June 08, 2020, 06:52:14 PM
cant run on win7
Title: Re: Hello World
Post by: Frank Kotler on June 09, 2020, 02:42:43 AM
No. int 0x10 is BIOS code.  It is 16 bit code.

If you want something for Windows, look into WriteFile.

Best,
Frank