问题
I am learning about boot sectors. I downloaded nasm-installer-x64.exe from the NASM website. My operating system is win7-64bit. When I run the following code it does not work correctly
mov ah, 0x0e;
mov al, the_secret;
int 0x10;
mov al, [the_secret];
int 0x10;
mov bx, [the_secret];
add bx, 0x7c00;
mov al, [bx];
int 0x10;
mov al, [0x7c1e];
int 0x10;
jmp $;
the_secret:;
db 'X';
times 510-($-$$) db 0;
dw 0xaa55;
回答1:
I don't believe there is anything wrong with times 510-($-$$) db 0
. It seems to me your are attempting to find the proper way to access the variable the_secret
and then display it to the screen. I'll provide one mechanism based on this attempt which has the most promise:
mov al, [the_secret];
int 0x10;
If you set up DS properly, set an origin point using org 0x7c00
and make sure BH is set to the page number you want to write to (you want 0) then the following code should work:
[bits 16] ; 16-Bit code
[org 0x7c00] ; Set the origin point to 0x7c00
start:
xor ax,ax ; We want a segment of 0 for DS for this question
mov ds,ax ; Set AX to appropriate segment value for your situation
mov es,ax ; In this case we'll default to ES=DS
mov bx,0x8000 ; Stack segment can be any usable memory
mov ss,bx ; This places it with the top of the stack @ 0x80000.
mov sp,ax ; Set SP=0 so the bottom of stack will be @ 0x8FFFF
cld ; Set the direction flag to be positive direction
mov ah, 0x0e
mov al, [the_secret] ; al = character from memory DS:[the_secret]
xor bh, bh ; bh = 0 = video page number
int 0x10;
jmp $
the_secret:;
db 'X';
times 510-($-$$) db 0
dw 0xAA55
The start up code sets DS to zero since we set an origin point of 0x7c00. The bootloader is loaded at 0x0000:0x7c00 (physical address 0x07c00). This ensures accessing the variable the_secret
will be done properly. mov al, [the_secret]
is the equivalent of saying mov al, ds:[the_secret]
. If the DS segment register is not set properly, and the origin point isn't set properly, the memory access will not read from the proper location.
INT 0x10/AH=0x0E requires a page number to be set. The first video display page is 0, BH should be set accordingly.
More on the other setup instructions can be found in my StackOverflow answer that contains General Bootloader Tips.
The code I have presented should display X
to the console if properly written to a disk image.
To assemble this code and produce a disk image (in my example a 720k floppy):
nasm -f bin bootload.asm -o bootload.bin
dd if=/dev/zero of=disk.img bs=1024 count=720
dd if=bootload.bin of=disk.img bs=512 count=1 conv=notrunc
The first command does the assembling of bootload.asm
into a flat binary file called bootload.bin
. The second command produces a zero filled disk image (disk.img
) of size 1024 * 720 (720kb floppy), and the last command copies 512 bytes of data from bootload.bin
to the first sector of the disk image. conv=notrunc
tells DD not to truncate the file after writing. If you were to leave that off disk.img
would be 512 bytes long after the bootsector was written.
来源:https://stackoverflow.com/questions/36052492/times-510-db-0-does-not-work