You are not logged in.
EDITED
To help you, I uploaded the code here https://github.com/babaliaris/bootloader
There are 3 branches, master, noorg-50kb and noorg-50bytes.
master-> second code in this post
noorg-50kb-> first code in this port
noorg-50bytes-> last code in this post
I'm trying to understand how to initialize the segments (ds, es, ss) in a bootloader but I can't make it work unless I use the [org 0x7c00] directive of the nasm assember.
The code below does not work:
First Code
1 [bits 16]
2 ;[org 0x7c00]
3
4 ;Alocate 50kb for the stack above the end of the 512 boot sector.
5 mov bp, 0x7e00 ;location where the boot loader code ends.
6 add bp, 0xC350 ; 50kb above 0x7e00
7 mov sp, bp
8
9 ;Clean the es and ss
10 xor ax, ax
11 mov es, ax
12 mov ss, ax
13
14 ;Initialize the ds
15 mov ax, 0x7c0
16 mov ds, ax
17
18 ;Call print_hex with in:0xabcd
19 mov bx, 0xabcd
20 call print_hex
21
22 ;Call printf with message hello
23 mov bx, hello
24 call printf
25
26 ;Load 5 sectors starting at sector 2 of cylinder 0 and head 0
27 mov al, 5
28 mov ch, 0
29 mov cl, 2
30 mov dh, 0
31 mov dl, 0x80
32 call read_disk
33
34 jmp $
35
36 %include "include/print_hex.asm"
37 %include "include/printf.asm"
38 %include "include/read_disk.asm"
39
40 hello: db "Hello World!", 0
41
42 times 510 - ($-$$) db 0
43 dw 0xaa55
but if I comment out the 16 line and uncomment the 2 line, then it works:
Second Code
1 [bits 16]
2 [org 0x7c00]
3
4 ;Alocate 50kb for the stack above the end of the 512 boot sector.
5 mov bp, 0x7e00 ;location where the boot loader code ends.
6 add bp, 0xC350 ; 50kb above 0x7e00
7 mov sp, bp
8
9 ;Clean the es and ss
10 xor ax, ax
11 mov es, ax
12 mov ss, ax
13
14 ;Initialize the ds
15 mov ax, 0x7c0
16 ;mov ds, ax
17
18 ;Call print_hex with in:0xabcd
19 mov bx, 0xabcd
20 call print_hex
21
22 ;Call printf with message hello
23 mov bx, hello
24 call printf
25
26 ;Load 5 sectors starting at sector 2 of cylinder 0 and head 0
27 mov al, 5
28 mov ch, 0
29 mov cl, 2
30 mov dh, 0
31 mov dl, 0x80
32 call read_disk
33
34 jmp $
35
36 %include "include/print_hex.asm"
37 %include "include/printf.asm"
38 %include "include/read_disk.asm"
39
40 hello: db "Hello World!", 0
41
42 times 510 - ($-$$) db 0
43 dw 0xaa55
The expected output is:
0xabdc. Hello World! Disk Error!
But I can't understand why the first try doesn't work... The lines
14 ;Initialize the ds
15 mov ax, 0x7c0
16 mov ds, ax
should do the trick without having to say [org 0x7c00] at the beginning.
Last Code
I also noticed that I can make it work If I change the lines
4 ;Alocate 50kb for the stack above the end of the 512 boot sector.
5 mov bp, 0x7e00 ;location where the boot loader code ends.
6 add bp, 0xC350 ; 50kb above 0x7e00
7 mov sp, bp
to allocate a smaller stack.
(By the way, the reason I use 0x7e00 + amount of allocation is that I believe
this is the address where the boot loader 512bytes ends and above that, there are 638kb free)
For example, if I change
6 add bp, 0xC350 ; 50kb above 0x7e00
to
6 add bp, 50 ; 50bytes above 0x7e00
then it works.
This seems like the first method (not using [org] but initializing ds manually) affects the way I initialize the bs and sp
but I can't understand what is happening at all...
These includes
36 %include "include/print_hex.asm"
37 %include "include/printf.asm"
38 %include "include/read_disk.asm"
just include some functions which use the stack.
Last edited by babaliaris (2023-01-09 01:41:49)
Github Account: github.com/babaliaris Arch General Guidelines
Favourite Distro: archlinux.org Arch Wiki
Offline
Why are we doing this?
This smells a little like an school assignment.
Why Arch Linux forums and not Stack Overflow?
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Why are we doing this?
I'm doing it for learning purposes.
Why Arch Linux forums and not Stack Overflow?
I'm banned at stack overflow. I'm not getting any upvotes in my questions...
Also, I thought that arch Linux could be a good place to ask this question since
a lot of people dive deep into operating systems.
I might send an email to the professor who teaches operating systems
at my University but I don't think he will write me back...
PS: This is not an assignment, just personal interest
Last edited by babaliaris (2020-08-10 20:53:40)
Github Account: github.com/babaliaris Arch General Guidelines
Favourite Distro: archlinux.org Arch Wiki
Offline
But I can't understand why the first try doesn't work... The lines
14 ;Initialize the ds 15 mov ax, 0x7c0 16 mov ds, ax
should do the trick without having to say [org 0x7c00] at the beginning.
EDITED
"org" directive affects all absolute addresses. E.g. when the code is loaded to 0:7C00 but assumes it's at 0, instruction pointer will get invalid address after "call" instruction.
Near call use relative address, it's not the reason.
But
5 mov bp, 0x7e00 ;location where the boot loader code ends. 6 add bp, 0xC350 ; 50kb above 0x7e00
leads to 16-bit overflow. Stack pointer value becomes 0x4150.
Last edited by dimich (2020-08-24 01:40:17)
Offline