Problem with Babystep tutorials

Programming, for all ages and all languages.
Post Reply
User avatar
tongko
Member
Member
Posts: 26
Joined: Wed Nov 07, 2012 2:40 am
Location: Petaling Jaya, Malaysia

Problem with Babystep tutorials

Post by tongko »

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: Select all

; 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: Select all

; 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<
h0bby1
Member
Member
Posts: 240
Joined: Wed Aug 21, 2013 7:08 am

Re: Problem with Babystep tutorials

Post by h0bby1 »

maybe you should put the [ORG 0x7C00] at the beginning of the file no ?
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: Problem with Babystep tutorials

Post by Nessphoro »

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: Select all

EXTERN cprint
EXTERM sprint
 
And then link the files as a flat binary at the address 0x7C00
newanabe
Member
Member
Posts: 46
Joined: Mon Aug 05, 2013 8:15 am

Re: Problem with Babystep tutorials

Post by newanabe »

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: Select all

[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
newanabe
Member
Member
Posts: 46
Joined: Mon Aug 05, 2013 8:15 am

Re: Problem with Babystep tutorials

Post by newanabe »

sorry I forgot to fill it properly
this is the new one, im distracted hehe

Code: Select all

[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
newanabe
Member
Member
Posts: 46
Joined: Mon Aug 05, 2013 8:15 am

Re: Problem with Babystep tutorials

Post by newanabe »

I think you cant load directly a data segment, change this

Code: Select all

mov DS, 0AC80h
to this

Code: Select all

mov AX, 0AC80h 
mov DS, AX
User avatar
tongko
Member
Member
Posts: 26
Joined: Wed Nov 07, 2012 2:40 am
Location: Petaling Jaya, Malaysia

Re: Problem with Babystep tutorials

Post by tongko »

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.
Post Reply