Hi again, I'm almost there. I don't know how many times I've said this, but anyway, I'm closer than before to finally print the MBR to the screen. The reason I left was because I was occupied with uni and I'm still but I wasn't making any progress with my code and it was taking too much time so put it on hold. After a few months I started again and I didn't wanted to just copy and paste someone else's code without understanding it so I didn't copy
Brendan's code (thanks anyway
Brendan) although in the end I ended up doing more or less copy and paste too from another person -.- I say more or less because I've modified it a bit but most of the instructions are the same. But in the literal meaning of the definition, yes that's what I did.
Since parts of the code weren't commented maybe because it might be something stupid that anyone should understand at first sight (except me), I pretty much flooded the code with comments and I wanted to show to you guys what I have until now. I've been studying how everything works in order to be able to comment pretty much everything. I know it's not a good practice in programming but I find assembly so exhaustive that I prefer to do it. If you care for looking at it and see that some of my comments are wrong then I would appreciate if you'd tell me so I can learn more. If you don't care, then no problem.
Well, here is the
boot.asm code:
Code: Select all
%define store_location 0x1000
%define drive 0x80
%define code_sector_location 2 ;This is index 0 so we are actually pointing to sector 1.
bits 16
org 0x7c00
jmp start
start:
mov ax, cs ;AX points to CS 0x7c0
mov ds, ax ;DS points to 0x7c0
mov es, ax ;ES contains what is on 0x7c0
mov al, 03h ;Text mode 25x80
mov ah, 0
int 10h
mov ax, store_location
mov es, ax ;Point ES to store_location (top of our stack)
mov cl, code_sector_location
mov al, 3 ;Number of sectors to read
call read_sectors ;Load sectors and put them into RAM address of store_location
jmp store_location:0000 ;Far jump to offset 0000 to the segment in RAM where we have stored our program code after reading from it disk
read_sectors:
mov bx, 0
mov dl, drive
mov dh, 0 ;Head to read
mov ch, 0 ;Track to read
mov ah, 2 ;To read sectors
int 0x13
jc error_reading
ret
error_reading:
mov si, error_msg
call print
mov ah, 0 ;Wait for key press
int 16h
int 19h ;Reboot but leave memory to where is pointing
print:
pusha ;Store all general-purpose registers on the stack
mov bp, sp ;Allocates in stack program variables
iterate:
lodsb ;Loads 8bit byte address to work with strings
or al, al ;Logic or to check if string already reached its end "0"
jz done_writing
mov ah, 0x0e ;To print character into screen
mov bx, 0
int 10h
jmp iterate
done_writing:
mov sp, bp ;Get variables out of stack
popa ;Retrieve all general-purpose registers of the stack with order LIFO ignoring SP
ret
error_msg db "An error occurred while reading sectors and loading them into RAM ", 10, 13, 0
times 510 - ($-$$) db 0
dw 0xaa55
Then it comes the code that is bigger than 512 bytes that I have put in sector 1 (index 0) I call the file
message.asm:
Code: Select all
[bits 16]
[org 0]
start:
mov ax, cs ;AX points to CS (0)
mov ds, ax ;Data segment is now pointing to (0)
mov es, ax ;ES points to (0) too
mov si, A
call print
mov si, B
call print
mov si, C
call print
mov si, D
call print
mov si, E
call print
mov si, F
call print
mov si, G
call print
mov si, H
call print
mov si, I
call print
mov si, J
call print
mov si, K
call print
mov si, L
call print
mov si, M
call print
mov si, N
call print
mov si, O
call print
mov si, P
call print
mov si, Q
call print
mov ah, 0 ;Wait for key press
int 16h
int 19h ;Reboot but leave memory to where is pointing so we can continue with our normal boot
print:
pusha ;Store all general-purpose registers on the stack
mov bp, sp ;Allocates in stack program variables
iterate:
lodsb ;Loads 8bit byte address from SI register to work with strings
or al, al ;Logic or to check if string already reached its end "0"
jz done_writing
mov ah, 0x0e ;To print character into screen
mov bx, 0
int 10h
jmp iterate
done_writing:
mov sp, bp ;Get variables out of stack
popa ;Retrieve all general-purpose registers of the stack with order LIFO ignoring SP
ret
A db " ___ ___ ___ ___ ___ ___ ___ ___ ", 10,13,0
B db " ___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___", 10,13,0
C db " / \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ \ ", 10,13,0
D db " \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ ", 10,13,0
E db " / \___/ \___/ \ ", 10,13,0
F db " \___/ \___/ ", 10,13,0
G db " / \ / \ ", 10,13,0
H db " \___/ Welcome to my bootloader! \___/ ", 10,13,0
I db " / \ Under construction / \ ", 10,13,0
J db " \___/ \___/", 10,13,0
K db " / \ Press any key to continue / \ ", 10,13,0
L db " \___/ with normal boot. \___/ ", 10,13,0
M db " / \___ ___/ \ ", 10,13,0
N db " \___/ \___ ___ ___ ___ ___ ___ ___/ \___/ ", 10,13,0
O db " / \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ \ ", 10,13,0
P db " \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ ", 10,13,0
Q db " \___/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ ", 10,13,0
Here you have a screenshot:
The second image was that I put it in a pen drive and booted the computer with it and the first one is in the Virtual Machine.
I still don't know how to display the MBR in the screen
. I could make this program to read it and store those 512 bytes it in say 0x3000, but then I don't know how to recursively access each byte from there. For example byte at position 0x3000:0000, then 0x3000:0001 and so on since those addresses are in hex and I have no idea on how to make a counter in hex since CX only counts integers.
Oh, if anyone mind, here are a few of the sources I've read before reaching that. I've read more but I don't have the links, sorry. The first link is pretty much what helped me. If it weren't for it I wouldn't have been able to get past the 512 bytes and load disk sectors correctly:
http://appusajeev.wordpress.com/2011/01 ... e-os-nasm/
http://www.assembly.happycodings.com/code7.html
http://www.htl-steyr.ac.at/~morg/pcinfo ... te6xo0.htm
http://www.skynet.ie/~darkstar/assembler/tut3.html
http://www.petesqbsite.com/sections/tut ... rial2.html
http://jwball.co.uk/2011/04/writing-tex ... real-mode/
http://geezer.osdevbrasil.net/osd/cons/index.htm
http://www.ragestorm.net/blogs/?p=18
http://frdsa.fri.uniza.sk/~janosik/Vyuk ... cture7.pdf
http://www.programmingforums.org/thread17429.html
http://www.osdever.net/tutorials/view/g ... evelopment
http://www.cs.virginia.edu/~evans/cs216/guides/x86.html
http://linuxgazette.net/issue77/krishnakumar.html
http://www.supernovah.com/Tutorials/Kernel1.php
https://en.wikibooks.org/wiki/X86_Assem ... nd_64_Bits
http://www.oocities.org/codeteacher/x86 ... l1012.html