OSDev.org
https://forum.osdev.org/

Problem with Babystep tutorials
https://forum.osdev.org/viewtopic.php?f=13&t=25941
Page 1 of 1

Author:  tongko [ Wed Nov 07, 2012 3:15 am ]
Post subject:  Problem with Babystep tutorials

Hi,
I'm totally new to assembly.
I've tried the example publish in Babystep 4, it turns out everything just working as expected.
When I start to break up the code, I've got funny result. I know I definitely did something wrong, but no clue to spot it.

what I did was splitting sprint and cprint to a new file name sprint.inc and include it in boot.asm (the original code).
I build it using NASM with this command: nasm boot.asm -f bin -o boot.bin
When I write it to image and boot the VM, I've got the character "W" fill up first few lines.

Here's the mess I've done #-o :

spirnt.inc
Code:
; sprint.inc -- no BIOS print

%macro      sprint 3
         mov      si, %1
         jmp      %%sprint
%%dochar:   cprint   %2, %3
%%sprint:   lodsb                        ; string char to AL
         cmp      al, 0
         jne      %%dochar               ; else we're done
         add      byte [%2], 1            ; down 1 row
         mov      byte [%3], 0            ; set cursor back to first column

         ret
%endmacro

%macro      cprint 2
         mov      ah, 0x0F               ; attrib = white on black
         mov      cx, ax                  ; save char/attribute
         movzx   ax, byte [%1]
         mov      dx, 160                  ; 2 bytes (char/attrib)
         mul      dx                     ; for 80 columns
         movzx   bx, byte [%2]
         shl      bx, 1                  ; times 2 to skip attrib

         mov      di, 0                  ; start of video memory
         add      di, ax                  ; add y offset
         add      di, bx                  ; add x offset

         mov      ax, cx                  ; restore char/attribute
         stosw                        ; write char/attribute
         add      byte [%2], 1            ; advance to right

         ret
%endmacro



boot.asm
Code:
; boot.asm

         jmp      kmain

%include "sprint.inc"

kmain:      
[ORG 0x7C00]                                    ; add to offsets
         xor      ax, ax                           ; make it zero
         mov      ds, ax                           ; DS=0
         mov      ss, ax                           ; stack starts at 0
         mov      sp, 0x9c00                        ; 200h past code start

         mov      ax, 0xb800                        ; text video memory
         mov      es, ax

         sprint      msg, ypos, xpos

         mov      ax, 0xb800                        ; look at video mem
         mov      gs, ax
         mov      bx, 0x0000                        ; 'W'=57 attrib=0F
         mov      ax, [gs:bx]

         mov      word [reg16], ax                  ;look at register
         call      printreg16

hang:
         jmp      hang

;-------------------------------------------------------------------------------------------

printreg16:
         mov      di, outstr16
         mov      ax, [reg16]
         mov      si, hexstr
         mov      cx, 4                           ; four places
hexloop:
         rol      ax, 4                           ; leftmost will
         mov      bx, ax                           ; become
         and      bx, 0x0f                        ; rightmost
         mov      bl, [si + bx]                     ; index into hexstr
         mov      [di], bl
         inc      di
         dec      cx
         jnz      hexloop

         mov      si, outstr16
         sprint      outstr16, ypos, xpos

         ret

;------------------------------------

         xpos      db 0
         ypos      db 0
         hexstr      db '0123456789ABCDEF'
         outstr16   db '0000', 0                  ; register value string
         reg16      dw 0                        ; pass values to printreg16
         msg      db "Welcome to Simple OS", 0
         
         times 510-($-$$) db 0                     ; pad subsequent 510 byte to 0
         db 0x55                                 ; Old BIOS need this to decide that
         db 0xAA                                 ; this is boot code.
;==================================


Any guru please help to point out the error and how should I do it in the right way? [-o<

Author:  h0bby1 [ Thu Aug 22, 2013 4:47 am ]
Post subject:  Re: Problem with Babystep tutorials

maybe you should put the [ORG 0x7C00] at the beginning of the file no ?

Author:  Nessphoro [ Thu Aug 22, 2013 6:33 pm ]
Post subject:  Re: Problem with Babystep tutorials

Thats not a very good way to do it.

You want to compile the two files separately into object files, and then have a header file that says something like
Code:
EXTERN cprint
EXTERM sprint


And then link the files as a flat binary at the address 0x7C00

Author:  newanabe [ Sun Sep 08, 2013 11:48 pm ]
Post subject:  Re: Problem with Babystep tutorials

Im glad to see somebody interested in ASM. My council is to keep it simpler possible in the beginning. Center your attention in the code, that means use only one file. I am programing from years in ASM, and all in the same archive. last week I used %include directive to can comment it eliminating that way a lot of code, but it would work without that macro too.

Code:
[BITS 16]
mov DS, 0AC80h
mov BX, 0B800h
mov ECX, 80*5
fill_first_5_lines:
mov word [DS:BX], 0F00h | "W"     ;((0AC80h << 4) + 0B800h) = 0B8000h
add BX, 2
loop fill_first_5_lines
hlt                            ;never forget the halt


I am not friendly with 16 bit mode, but this should work, and you can compare the amount of code in the example you're using and this one. just put it in Notepad and save it like bootW.asm (.txt would work too) and run from the terminal "nasm -f bin bootW.asm -o boot.bin". I didn't tested it cuz I have not the programs installed in my internet-pc.
If this work for you, keep it simple, that's my council for an ASM learner

Author:  newanabe [ Sun Sep 08, 2013 11:55 pm ]
Post subject:  Re: Problem with Babystep tutorials

sorry I forgot to fill it properly
this is the new one, im distracted hehe
Code:
[BITS 16]
beginning_of_boot_sector:
mov DS, 0AC80h
mov BX, 0B800h
mov ECX, 80*5
fill_first_5_lines:
mov word [DS:BX], 0F00h | "W"     ;((0AC80h << 4) + 0B800h) = 0B8000h
add BX, 2
loop fill_first_5_lines
hlt                            ;never forget the halt
times 510-($-beginning_of_boot_sector) db 0
db 55h, 0AAh

Author:  newanabe [ Sun Sep 08, 2013 11:58 pm ]
Post subject:  Re: Problem with Babystep tutorials

I think you cant load directly a data segment, change this
Code:
mov DS, 0AC80h
to this
Code:
mov AX, 0AC80h
mov DS, AX

Author:  tongko [ Wed Nov 15, 2017 12:36 am ]
Post subject:  Re: Problem with Babystep tutorials

Wow! I never expect to get answers after more than half year 8) BTW, thank you for all you answer, I truly appreciate your help. Now that I have a spare notebook which I throw in latest Ubuntu, I started to revisit OS Dev again and surely will ask more questions.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/