【发布时间】:2021-09-26 03:44:59
【问题描述】:
[org 0x7c00] ; declares the origin to 0x7c00
mov bp, 0x7c00 ; move the memory adress 0x7c00 to the base pointer, CS:IP always points to physical address 0x07C00.
mov sp, bp ; move the base pointer to the stack pointer
Hello_World: ; declares the label of the hello World position
db 'Hello World!',0 ; declares the string Hello World!
mov bx, Hello_World ; move Hello_World labels position to bx[16 bit]
call printstring ; calls the printsctring function
printstring: ; declares position of printstring function
mov ah, 0x0e
.loop: ; declares position of loop
cmp [bx], byte 0 ; compares a size of 0 bytes to bx
je .exit ; if the result is equal jump to exit
mov al, [bx] ; declares move value at bx to al.
int 0x10 ; declares print
inc bx ; increment the bx as a for loop
jmp .loop ; jump to the for loop
.exit: ; labels position of exit
ret ; returns from read_disk to its call
mov [boot_disk], dl ; moves dl contents to boot disk error
boot_disk: ; declares boot_disk position
db 0 ; defines a byte of 0
; boot disk should now store the disk number inside the defined byte
program_space equ 0x7e00 ; program space variable equates to 0x7e00.
; 0x7e00 is 512 bytes after 0x7c00 in memory
call read_disk ; calls read_disk function position
read_disk: ; declares read_disk position
mov bx, program_space ; moves the program space to bx
mov al, 5 ; reads 4 sectors aka 2000 bytes, or 2 kilobytes
mov dl, [boot_disk] ; moves the value of boot disk into dl
mov ch, 0x00 ; moves cyclinder 0 of hard drive into ch
mov dh, 0x00 ; moves cyclinder 0 of hard drive into dh
mov cl, 0x02 ; Moves 2 cylinders to cl
int 0x13 ; interrupt commands the bios to read the disk
jc disk_read_failed ; jumps conditionaly to disk read failed
ret ; returns to position of call printstring
disk_read_error: ; declares disk_read_error position
db 'disk read failed',0 ; defines byte as disk read failed
disk_read_failed: ; declares the label of disk_read_failed position
mov bx, disk_read_error ; moves disk_read_error to bx
call printstring ; calls the printing of the bx within a for loop.
jmp $
jmp $ ; declares infinite loop
times 510-($-$$) db 0 ; declares bootloader size as 510 bites
dw 0xaa55 ; declares this as a bootloader and uses 2 bites
; total bites = 512 bites = 2 bites + 510 bites
【问题讨论】:
-
快速浏览一下,发现您将“Hello World”数据放在代码旁边。处理器执行“mov sp,bp”指令后,会将“Hello World”数据字节解释为指令。您必须将数据放在执行流之外的某个位置。
标签: assembly nasm x86-16 bootloader