From bdca5e65da76c2d67a760311090bdf4272c0e2a9 Mon Sep 17 00:00:00 2001 From: Carlos Fenollosa Date: Sun, 5 Oct 2014 11:11:03 +0200 Subject: [PATCH] lesson 3, boot sector with memory addressing --- .../.boot_sect_memory.asm.swp | Bin 0 -> 12288 bytes 03-bootsector-memory/README.md | 39 ++++++++++++++- 03-bootsector-memory/boot_sect_memory.asm | 47 ++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 03-bootsector-memory/.boot_sect_memory.asm.swp create mode 100644 03-bootsector-memory/boot_sect_memory.asm diff --git a/03-bootsector-memory/.boot_sect_memory.asm.swp b/03-bootsector-memory/.boot_sect_memory.asm.swp new file mode 100644 index 0000000000000000000000000000000000000000..2d40fbce749278f0987891a6bf763725b9f5c451 GIT binary patch literal 12288 zcmeI2O>YxN7{@0ZX=qU^aei>4CXr%$Z4;$xPQ0kZNC;JF2@;Cv?CdzZu)DLGnZ-_9 z!L?GaoH%ml$_Ic?zy~NjRHfIdt$OA^@q*nZ5mIl6XQiL_edhoCW_++baddrnl{Pv9 zf$g*qizj zm)>>vGN0CQ*SNN?y|J0hyV0N>8{?f;xX>*Z>Ti z5`%s(3MxKrouyAc{)Vlv0XDz}*Z><~18jf|umLvk9vSe}g1C=*Pc$`moBhIn_RT5p z*Z><~18jf|umLu}2G{@_U;}J`4X}ZC(11*Yxb}e%%Zmt)|NmeA{{QPEA^rp}!6Wb^ z_yIWZE!Y9q!4>fLheA99-va|i;482NE`v?*&nY3Ef+yfHcnE$1_dpFiNWmF!5-fl} zkm+~u68r{!0WZMM;5m2zw!s#-40<5c@fqNa4X^<=zy{a=8(;%$fDN#L|7Rem6DjSqL8rRsCA=taJWgPB%fxElu0y>q$ixqMmPvT3q(oai0wHL~rcCL>+G;YRY2eC0gtmC+JUR=HTo_*4hF58Q z&~HW~r-P}GfV??)WV+0n5YKAYw1v9oO2Xpt!-8kIkCNOE!JP>{ON}u_U!nekiP8*Z zGsG#%>tH;V^PG~9C`_gDjm`nu*)n5&SKGC@Y8>T8h6xeo-0SF#*-aXHjBbW`jBYC% zvIiH33tgTSu}tGMQ)w9O@T@Y{VhuXUs* +and look at the figure with the memory layout. + +I could just go ahead and tell you that it starts at `0x7C00`, but it's +better with an example. + +We want to print an X on screen. We will try 4 different strategies +and see which ones work and why. + +First, we will define the X as data, with a label: +```nasm +the_secret: + db "X" +``` + +Then we will try to access `the_secret` in many different ways: + +1. `mov al, the_secret` +2. `mov al, [the_secret]` +3. `mov al, the_secret + 0x7C00` +4. `mov al, 2d + 0x7C00`, where `2d` is the actual position of the X in the binary + +Take a look at the code and read the comments. + +Compile and run the code. You should see a string similar to `1[2ยข3X4X`, where +the bytes following 1 and 2 are just random garbage. + +If you add or remove instructions, remember to compute the new offset of the X +by counting the bytes, and replace `0x2d` with the new one. + +~~~~~ +This whole tutorial is heavily inspired on that document. Please read the +root-level README for more information on that. diff --git a/03-bootsector-memory/boot_sect_memory.asm b/03-bootsector-memory/boot_sect_memory.asm new file mode 100644 index 0000000..b67f4de --- /dev/null +++ b/03-bootsector-memory/boot_sect_memory.asm @@ -0,0 +1,47 @@ +mov ah, 0x0e + +; attempt 1 +; Fails because it tries to print the memory address (i.e. pointer) +; not its actual contents +mov al, "1" +int 0x10 +mov al, the_secret +int 0x10 + +; attempt 2 +; It tries to print the memory address of 'the_secret' which is the correct approach. +; However, BIOS starts loading at address 0x7c00 +; so we need to add that padding beforehand. We'll do that in attempt 3 +mov al, "2" +int 0x10 +mov al, [the_secret] +int 0x10 + +; attempt 3 +; Add the BIOS starting offset 0x7c00 to the memory address of the X +; and then dereference the contents of that pointer +mov al, "3" +int 0x10 +mov bx, the_secret +add bx, 0x7c00 +mov al, [bx] +int 0x10 + +; attempt 4 +; We try a shortcut since we know that the X is stored at byte 0x2d in our binary +mov al, "4" +int 0x10 +mov al, [0x7c2d] +int 0x10 + + +jmp $ + + +the_secret: + ; ASCII code 0x58 is stored just before the zero-padding + ; on this code that is at byte 0x2d (check it out using xdd) + db "X" + +times 510-($-$$) db 0 +dw 0xaa55