First correct some bugs.
start:
mov si, 7F00h ;; set stack pointer after our bootloader
mov ax, 0h
mov ds, ax ;; set DS to 0;
mov di, 7C00h ;; set Data pointer to memory location where is our bootloader loaded
The stackpointer on x86 is held in the SP
register. You've loaded SI
, a general purpose register that your program doesn't use at all hereafter.
Your bootloader program sits in memory from 7C00h to 7DFFh. If you put your stack behind the programcode and start with a stackpointer at 7F00h, you will have 256 bytes of stack (from 7E00h to 7EFFh) before you run into the program itself. If the forementioned error were corrected, this is exactly what would happen because of a second error further down in the program.
Each word you read from the program memory, you push it on the stack, but you leave it there and so the stack fills up to the point that you're pushing on top of the program memory!
Either pop dx
to balance the stack on each iteration of the program (write it just above .loopend:
), or much better don't use the stack this way since there are enough registers free to store that particular value.
Given that your program is a bootloader, there are no segment registers that you can trust to have a defined value! If you're going to setup a stack, you'll need to also initialize the SS
segment register. It's important to do that in the instruction directly above initializing the SP
register.
start:
xor ax, ax ; This is shorter/faster than "mov ax, 0h"
mov ds, ax
mov ss, ax
mov sp, 7F00h ; set stack pointer after our bootloader
mov di, 7C00h ; set Data pointer to memory location where is our bootloader loaded
Keep things together.
You've put the setup of the BIOS.Teletype function number outside of the loop. Although of course this is not wrong, it does diminish the readability of the program. I prefer to always have the mov ah, 0Eh
just above of the int 10h
.
Get rid of redundant things.
The mov al, 0
instruction doesn't serve any purpose for the BIOS delay function.
The ds:
segment override prefix in mov dx, [ds:di]
is redundant since DS
is the segment that will be used by default. Writing it increases the code size by 1 byte.
The BIOS.Teletype function does not require nor clobber the CX
register. You don't have to push cx
... pop cx
. Your mention of CX
being a volatile register does not apply when dealing with an API like BIOS. Just look at the registers in and registers out.
If you look up the Teletype function you will notice that it also requires you to setup the BH
register with the DisplayPage and if on a graphics screen the BL
register with the Color.
Use the best loop construct.
Currently your program uses a While constructs that requires 2 jumps on each iteration. Jumps are expensive in terms of execution time and so we always try to have as few as possible.
If you know that the loop is going to be run at least once then a Repeat-Until construct is better suited.
xor cx, cx
.loopstart:
... ; Counting upward [0,15]
inc cx
cmp cx, 16
jb .loopstart
If you know that the body of this loop does not depend on the actual value in the loop counter then a slightly better version will be:
mov cx, 16
.loopstart:
... ; Counting downward [16,1]
dec cx
jnz .loopstart
See the opportunities to write compacter code.
pop dx
rol dx, 1
push dx
and dx, 0000000000000001
add dx, 30h
push cx
mov al, dl
int 10h
pop cx
When you know that the BIOS.Teletype function expects the character in the AL
register, you should strive to do these calculation straight on the accumulator which additionaly uses shorter encodings.
pop ax
rol ax, 1
push ax
and al, 1
add al, "0"
mov ah, 0Eh
int 10h
We can go a bit further here. Instead of placing the data on the stack (push dx
), we can hold it in the BP
register (mov bp, [di]
). Use the registers that are at your disposal!
With some more clever re-arranging the above snippet becomes:
rol bp, 1 ; Produces a CF
mov ax, 0E00h ; Function number in AH, zeroing AL
adc al, "0" ; 0 + "0" + CF=0 ==> "0"
int 10h ; 0 + "0" + CF=1 ==> "1"
The conditional jumps can jump 128 bytes backwards (x86-16).
add di, 2h
cmp di, 7E00h ;; are we at the end of our bootloader (7c00h + 200h )?
je .hlt ;; if yes halt
jmp .printMemoryValue ;; print next memory location ( di is increased by 2 )
.hlt:
hlt ;; ende
Inverse the conditioncode and you'll no longer need the direct jump nor the extra label.
add di, 2
cmp di, 7E00h
jne .printMemoryValue
hlt
For robustness the jne
should become jb
. Sometimes things go wrong and so it could be, that the expected value of 7E00h never occurs producing an infinite loop! That's why prudent programmers prefer testing for less/below and greater/above conditions.
This is the complete code with all of the above applied:
start:
xor ax, ax ; This is shorter/faster than "mov ax, 0h"
mov ds, ax
mov ss, ax
mov sp, 7F00h ; set stack pointer after our bootloader
mov di, 7C00h ; set Data pointer to where is our bootloader loaded
.printMemoryValue:
mov cx, 0006H ; CX:DX = 00068480h Pause for about 0.4 sec
mov dx, 8480H
mov ah, 86h ; BIOS.Delay
int 15h
mov bp, [di] ; Moving content of memory location in BP
mov bx, 0007h ; Display page 0 in BH, Attribute WhiteOnBlack in BL
mov cx, 16
.loopstart:
rol bp, 1 ; Produces a CF
mov ax, 0E00h ; Function number in AH, zeroing AL
adc al, "0" ; 0 + "0" + CF=0 ==> "0"
int 10h ; 0 + "0" + CF=1 ==> "1"
dec cx
jnz .loopstart
mov ax, 0E0Dh ; Newline is carriage return plus linefeed
int 10h
mov ax, 0E0Ah
int 10h
add di, 2
cmp di, 7E00h
jb .printMemoryValue
hlt ; ende
times 510-($-$$) db 0
dw 0AA55h ; => 55h 0AAh (little endian byte order)
4.How to properly format asm code ?
Everybody has a personal style. Most people however like to use the nice tabular format that you see in my code examples.
What you should do is being consistent when it comes to number representations.
Choose between the hex prefix 0x
or the hex suffix h
, but try not to mix both in the same program. This is especially true in a short program.
Don't forget to write the affix for numbers that need it.
and dx, 0000000000000001 --> and dx, 0000000000000001b
Write as many hex digits as the register can take.
mov ax, 0h --> mov ax, 0000h
cmp cx, 00010h --> cmp cx, 0010h
add dx, 30h --> add dx, 0030h
mov al, 000Ah --> mov al, 0Ah
mov al, 000Dh --> mov al, 0Dh
add di, 2h --> add di, 0002h
Don't express numbers that don't really need it in hexadecimal.
mov ax, 0h --> mov ax, 0
cmp cx, 00010h --> cmp cx, 16
add di, 2h --> add di, 2
Sometimes expressing a number as a character improves readability.
add dl, 30h --> add dl, "0"