Simple assembly code error

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

Simple assembly code error

Post by bubux »

My machine is WinXP, Nasm.
I've been trying to compile a simple boot loader using NASM:

Code: Select all

[ORG 7C00h]      ; Onde a BIOS nos coloca

; ---------------------------------------------------------
; Programa principal
; ---------------------------------------------------------


inicio:
   ; Configurar a pilha.
   ; N?o podemos permitir interrup??es durante a configura??o
   cli         ; Disabilitar interrup??es
   mov ax, 0x9000      ; Por a pilha em 9000:0000
   mov ss, ax      ; Transferir o endere?o para o registrador
         ;   do segmento da pilha (SS)
   mov sp, 0      ; Zerar ponteiro do topo da pilha
   sti         ; Habilitar interrup??es (SeT Interrupts bit)

   mov [bootdrv], dl   ; DL indica o drive de boot
   call carregar      ; Chamar a fun??o que reseta a controladora
            ; e faz a leitura de setores do disco
   jmp far 1000h:0      ; Salta para o buffer com o c?digo

; ---------------------------------------------------------
; Fun??es e vari?veis do nosso bootstrap
; ----------------------------------------------------------

bootdrv db 0      ; A identifica??o do drive de boot

carregar:
   push ds      ; Preserva o valor do segmento de dados na pilha

.reset:
   mov ax, 0      ; Servi?o que reseta o sistema de disco
   mov dl, [bootdrv]   ; Drive que deve ser resetado
   int 13h      ; INT 13 faz o reset
   jc .reset      ; Falhou -> Tentar novamente (jc=jump on carry)

   pop ds         ; Retoma o valor do segmento de dados da pilha

.leitura:
   mov ax,0x1000      ; O buffer deve ficar em 1000:0000
   mov es,ax      ; Transfere 1000 para ES (Extra Segment)
   mov bx, 0      ; e transfere 0 de deslocamento para BX
         ; O resultado ? ES:BX com 1000:0000

   mov ah, 2      ; Servi?o 2 da INT 13 => Ler setores de disco
   mov al, 5      ; Ler 5 setores (? mais do que suficiente)
   mov cx, 2      ; Cilindro=0, Setor=2
   mov dh, 0      ; Cabe?a=0
   mov dl, [bootdrv]   ; Drive=drive de boot
   int 13h      ; Leia! ES:BX = dados do disco
   jc .leitura      ; falhou -> Tente novamente

   retn         ; Terminada a leitura, retornar

times 512-($-$$)-2 db 0   ; Preenche com 0 at? setor ter 512 bytes
   dw 0AA55h      ; P?e a assinatura do setor de boot
The error is:

Code: Select all

E:\MyOS\NumaBoa>nasm loader3.asm -f bin -o loader3.bin
loader3.asm:21: error: mismatch in operand sizes
The code is suposed to load this kernel in sector 2 (compiles ok using nasm):

Code: Select all

   mov ax, 1000h      ; P?e endere?o em AX
   mov ds, ax      ; Atualiza o segmento de dados
   mov es, ax      ; Atualiza o segmento extra

   mov si, msg      ; Aponta para a string da mensagem 
   call poeString      ; Chama a fun??o para imprimir a mensagem

pendura:      ; Apenas pendura
   jmp pendura


; Imprime uma string terminada em 0 (SI=ponteiro para a string)
poeString:
   lodsb         ; Copia o byte em DS:SI para AL e incrementa SI
   or al,al      ; Verifica se o byte lido ? zero
   jz short volta      ; Se sim, salta para volta
   mov ah,0x0E      ; Servi?o 0E da INT 10 da BIOS (Imprimir caracter)
   mov bx,0x0007      ; Imprimir branco em fundo preto
   int 0x10      ; Imprimir caracter
   jmp poeString      ; Pegar pr?ximo caracter
volta:
   retn         ; Terminada a tarefa, voltar ao ponto de chamada

msg     db 'Sistema Operacional NumaBoa II',13,10,0

Plz help fixing the loader! 8)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Simple assembly code error

Post by Solar »

1) Keeping your comments in English greatly helps your chances of others being able to make sense of your code once you seek help on internet forums. ;-) (I am German myself, and religiously comment in English nevertheless...)

2) The error message is quite clear: In line 21 of the file loader3.asm, there is a mismatch in the operand sizes.

Line 21 of the assembly source is

Code: Select all

jmp far 1000h:0 
I could start guessing, but I'd rather pass the ball to one of the (N)ASM gurus here. ;)
Every good solution is obvious once you've found it.
Kemp

Re:Simple assembly code error

Post by Kemp »

I believe the type of jump you are attempting has special syntax that is detailed in the NASM FAQ. Basically the jump location has to be cast (right word?) to a QWORD.
bubux

Re:Simple assembly code error

Post by bubux »

Thankyou for replies. The code is commented in portuguese. Sorry.
I'm looking for a VERY simple start code to star OS dev studies. So i was trying to make that one work.

I'll try to get another code to start working. My target is coding the OS in C. I know i'll have to do some assembly also.

Thanks for atention.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Simple assembly code error

Post by Solar »

bubux wrote: I'm looking for a VERY simple start code to star OS dev studies. So i was trying to make that one work.
FAQ: Rolling your own bootloader
FAQ: Some small kernels with source
FAQ: GRUB ready-made bootloader
FAQ: BareBones - from BIOS to [tt]int main()[/tt] in ten minutes

I hope one of those links helps you. ;)
Every good solution is obvious once you've found it.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:Simple assembly code error

Post by bubach »

For a very, very simple bootsector that loads a C kernel, check this tutorial:
http://www.osdever.net/tutorials/Xosdev.zip?the_id=13
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Simple assembly code error

Post by Pype.Clicker »

Kemp wrote: I believe the type of jump you are attempting has special syntax that is detailed in the NASM FAQ. Basically the jump location has to be cast (right word?) to a QWORD.
I might need to refresh my NASM (and unfortunately don't have any ASM book under hand) but i don't remind something like that was required ... By the way, you know that depending on the format mode, NASM may prefer to output 16 bits or 32 bits code. Did you checked it generated the proper one ?

Could it be that it just don't like "1000h" and rather prefer "0x1000" ? Or could you be using an old-and-buggy version of NASM that should be replaced ?

Oh, and just a thought: your stack is going to wrap to 0x9000:FFFE right at the first 'push', which means you'll be trashing the extended bios data area while loading ...
Kemp

Re:Simple assembly code error

Post by Kemp »

Actually I may have been talking about completely the wrong thing and as the NASM homepage is now broken I can't check :( I believe I was actually talking about a jump from 16bit code to 32bit code possibly, sorry.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Simple assembly code error

Post by Brendan »

Hi,
bubux wrote:

Code: Select all

   jmp far 1000h:0      ; Salta para o buffer com o c?digo
bubux wrote:The error is:

Code: Select all

E:\MyOS\NumaBoa>nasm loader3.asm -f bin -o loader3.bin
loader3.asm:21: error: mismatch in operand sizes
Try this:

Code: Select all

   jmp 1000h:0      ; Salta para o buffer com o c?digo
NASM doesn't need the "far" to tell it it's a far jump (and probably thinks "far" is a label or something)....


For everyone else, to jump from 16 bit code to 32 bit code you'd use "jmp dword SEG:OFFSET" - typically done after enabling protected mode (but not necessary if "OFFSET" is less than 64 KB as a 16 bit far jump will work fine).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
bubux

Re:Simple assembly code error

Post by bubux »

Thanks for your replies!
I'm so sorry for the comments being in portuguese. The guy who wrote the tutorial that i was following was using an old NASM.

Anyway, i've been wondering around Bona Fide and found Xosdev (http://www.osdever.net/tutorials/Xosdev.zip?the_id=13) that bubach point to me. And I decided to follow that tutorial since the guy explains alot more and goes deeper in details, yet, not so deep to confuse me.

Now I have one question! How can I setup his source code to get my kernel running. Because the turorial comes with 2 files:
- boot.asm (bootloader)
- k_init.asm (wich i believe is 32bit assembly code that will call my k_main() c function)

The problem is the tutorial don't tell me how to compile and copy the k_main.c file into the floppy.

For example:
- I know boot.asm is compiled with nasm and should go in 1st sector of floppy.
- I think that k_init.asm will go in the beggining of second sector because it will call k_main() C fucntion.
- Now where should I copy my compilation of k_main.c???????


Is this any near from being correct? Plz help me!

I just want my kernel.c file to write a black/white hello world to stimulate me!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Simple assembly code error

Post by Pype.Clicker »

ch02/basic_kernel.txt should tell it all ...

-{Step by Step}-
1-compile all *.c files
>gcc *.c

2-compile all asm files into a format like aout (not bin, C doesn't
output to bin by default)
>nasm *.asm -f aout

3-link all C files and asm files together into a file(ie:kernel.o)
>ld -T linkscript.ld -o kernel.o a.o b.o c.o
bubux

Re:Simple assembly code error

Post by bubux »

Ok I got it running in a floppy! At least Bochs could run it!

For those who are experiencing dificulties in getting the first "hello world" done (just for motivation), I atachted a file with everything working ok. All credits go to xosdev tutorial.

To get it running you will need just a floppy disk. But if you want to develop your own OS from it, you need to install in your computer: NASM, DJGPP (to compile c files) and Bochs (recomended).

Thank you all for attention! Now I hope to stop asking noob questions and lets get to work! \o/

;D

Ps. This topic can be closed.
Post Reply