Is this going to work?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
ghostlyfoot
Posts: 5
Joined: Mon Apr 09, 2012 8:40 pm
Location: USA, Mid-south (ie. Mississippi, Tennesee, Arkansas)

Is this going to work?

Post by ghostlyfoot »

I need to know if this works so I know if the tools I am using are faulty. I have been using nasm and partcopy, but it seems to only show a blank prompt when I boot this from a usb key. It is supposed to print a welcome message to the screen.

Code: Select all

mov si, msg
call print_string
hang: 
	jmp hang

print_string:
	lodsb
	cmp al, 0
	je print
	ret
print:
	mov ah, 0x09
	int 0x10
	jmp print_string

msg db 'Welcome to OS', 0

times 510-($-$$) db 0
dw 0xAA55
User avatar
GAT
Member
Member
Posts: 75
Joined: Wed Nov 30, 2011 9:51 pm
Contact:

Re: Is this going to work?

Post by GAT »

Almost, where it says

Code: Select all

cmp al,0
je print
Change that to

Code: Select all

cmp al,0
jne print
I think that should work.
d3: virtualizing kernel in progress
https://github.com/WizardOfHaas/d3/
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: Is this going to work?

Post by Rudster816 »

1. You need to setup your segments
2. You need to setup a stack for the BIOS to use
3. You're using the wrong AH value to print a character
4. What ghostlyfoot said

Because I'm very bored right now:

Code: Select all

ORG 0x7C00
BITS 16

jmp 0x0:start
	
start:
	cli
	xor ax, ax
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	mov ss, ax
	mov sp, 0x7C00
	sti

	mov si, msg
	call print_string
	
	cli
	hlt

print_string:
	lodsb
	test al, al
	jnz print
	ret
print:
	mov ah, 0xE
	int 0x10
	jmp print_string

msg db 'Welcome to OS', 0

times 510-($-$$) db 0
dw 0xAA55
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Is this going to work?

Post by Chandra »

Ah, let me make it concise. Rewrite everything. OSDEV Wiki at your service!
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Is this going to work?

Post by qw »

Rudster816 wrote:

Code: Select all

	cli
	hlt
Some emulators stop updating the screen if you do that, and you may not see the printed message at all. You'd better try this:

Code: Select all

here:
	hlt
	jmp here
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Is this going to work?

Post by Combuster »

Which doesn't fix the problem at all if interrupts are already off. You'll probably want

Code: Select all

CLI
JMP $
for QEMU-safe debugging purposes.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
ghostlyfoot
Posts: 5
Joined: Mon Apr 09, 2012 8:40 pm
Location: USA, Mid-south (ie. Mississippi, Tennesee, Arkansas)

Re: Is this going to work?

Post by ghostlyfoot »

This is what I managed to put together based on the wiki and your advice:

Code: Select all

bits 16
org 0x7C00

SECTION .text

jmp begin

begin:
	jmp setup_stack
setup_stack:
	mov esp, stack_end
	jmp next

next:
	mov si, msg
	call print_string
	cli
	jmp $

print_string:
   lodsb
   cmp al, 0
   jne print
   ret
print:
   mov ah, 0x09
   int 0x10
   jmp print_string

SECTION .bss

msg db 'Welcome to OS', 0

stack_begin:
	resb 4096
stack_end:

times 510-($-$$) db 0
dw 0xAA55
It gives me many, many, many warnings when i assemble it, though. What is the problem?
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Is this going to work?

Post by VolTeK »

Why don't you read the warnings. :roll:
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: Is this going to work?

Post by Rudster816 »

ghostlyfoot wrote:This is what I managed to put together based on the wiki and your advice:

-SNIP-

It gives me many, many, many warnings when i assemble it, though. What is the problem?
You failed to take a lot of my advice, and your code is far worse then what you had to begin with. I'll do a rundown the problems (there's a lot)...

1. You still fail to setup your segments before doing stuff that would require that
2. You have three useless jmp instructions (the first three).
3. You're still using the wrong AH value to print a character
4. You need to create a flat binary, there are no .bss or .text sections
5. Boot sectors are only 512 bytes long, so even though I believe the resb X works in binary mode (I believe its equal times X db 0), you can't have 4kb worth of 0's in a 512 byte boot sector.
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: Is this going to work?

Post by Kazinsal »

Rudster816 wrote:4. You need to create a flat binary, there are no .bss or .text sections
NASM interprets .text meaning everything from the org offset until the .data section, .data meaning everything from there until the end of the binary, and .bss meaning locations in memory after the end of the binary. So, as misused as they are, they're still kind of correct.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Is this going to work?

Post by qw »

Combuster wrote:Which doesn't fix the problem at all if interrupts are already off.
True, interrupts should be enabled. The HLT is not really necessary (JMP $ is just fine) but it reduces power consumption (on real hardware).
User avatar
brain
Member
Member
Posts: 234
Joined: Thu Nov 05, 2009 5:04 pm
Location: UK
Contact:

Re: Is this going to work?

Post by brain »

Hobbes wrote:
Combuster wrote:Which doesn't fix the problem at all if interrupts are already off.
True, interrupts should be enabled. The HLT is not really necessary (JMP $ is just fine) but it reduces power consumption (on real hardware).
It also reduces cpu usage of your emulator, on emulated hardware :-)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Is this going to work?

Post by Combuster »

Well what matters more after a crash, debug info or a lower power consumption? :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
ghostlyfoot
Posts: 5
Joined: Mon Apr 09, 2012 8:40 pm
Location: USA, Mid-south (ie. Mississippi, Tennesee, Arkansas)

Re: Is this going to work?

Post by ghostlyfoot »

Code: Select all

bits 16 ; 16 bit REAL MODE
org 0x07C00 ; originates at 0x07C00

jmp 0x0:start ; jump to starting point at 0x0 offset start

start: ; starting point
	; setup segments
	cli ; disable interrupts
	xor ax, ax ; make ax 0
	mov ds, ax ; make ds 0
	mov es, ax ; make es 0
	mov fs, ax ; make fs 0
	mov gs, ax ; make gs 0
	mov ss, ax ; make ss 0
	mov sp, 0x7C00 ; tell bios where the stack is? 
	sti ; enable interrupts

	mov si, msg ; move our message into si
	call print_string ; let's print

	cli ; disable interrupts so nothing 'interrupts' our hang
	jmp $ ; keep jumping to this line

print_string: ; will print a string in si
	lodsb ; get si and put it in al
	cmp al, 0 ; is al null?
	jne print ; if it isn't, jmp to print
	ret ; return to calling method
print: ; called by print_string
	mov ah, 0xE ; tell bios we want to print
	int 0x10 ; PRINT!
	jmp print_string ; loop

msg db 'Welcome to OS', 0 ; make a message and name it 'msg'

times 510-($-$$) db 0 ; fill with 0s
dw 0xAA55 ; siggy
Did I get all of this right (the comments)? Also, I know you use int 0x13 to load bytes, but I am using a USB flash drive and all of the sample code seems oriented toward a floppy. Can i use the same sectors per track, etc.?

EDIT: I am now using nasm and dd on either ubuntu or trisquel linux depending on the mood im in.
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: Is this going to work?

Post by evoex »

berkus wrote:
Combuster wrote:Well what matters more after a crash, debug info or a lower power consumption? :wink:
Imagine an unattended PC crashing, setting its power consumption to the maximum (CPU full speed etc), and then staying in this condition until a human technician checks it. Maybe weeks.
Of course, you could also just use something like:

Code: Select all

#ifndef NDEBUG
  jmp $
#else
  hlt
#endif
Post Reply