From dde61c13e7fe20cb218834a07888578dcc6cc0aa Mon Sep 17 00:00:00 2001 From: Carlos Fenollosa Date: Sun, 5 Oct 2014 12:03:44 +0200 Subject: [PATCH] lesson 4, manipulating the stack --- 04-bootsector-stack/README.md | 13 ++++++++ 04-bootsector-stack/boot_sect_stack.asm | 42 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 04-bootsector-stack/README.md create mode 100644 04-bootsector-stack/boot_sect_stack.asm diff --git a/04-bootsector-stack/README.md b/04-bootsector-stack/README.md new file mode 100644 index 0000000..8d02f90 --- /dev/null +++ b/04-bootsector-stack/README.md @@ -0,0 +1,13 @@ +*Concepts you may want to Google beforehand:stack* + +The usage of the stack is important, so we'll write yet another boot sector +with an example. + +Remember that the `bp` register stores the base address (i.e. bottom) of the stack, +and `sp` stores the top, and that the stack grows downwards from `bp` (i.e. `sp` gets +decremented) + +This lesson is quite straightforward, so jump ahead to the code. + +I suggest that you try accessing in-stack memory addresses by yourself, +at different points in the code, and see what happens. diff --git a/04-bootsector-stack/boot_sect_stack.asm b/04-bootsector-stack/boot_sect_stack.asm new file mode 100644 index 0000000..37399f0 --- /dev/null +++ b/04-bootsector-stack/boot_sect_stack.asm @@ -0,0 +1,42 @@ +mov ah, 0x0e + +mov bp, 0x8000 ; this is an address far away from 0x7c00 so that we don't get overwritten +mov sp, bp + +push 'A' +push 'B' +push 'C' + +; to show how the stack grows downwards +mov al, [0x7ffe] ; 0x8000 - 2 +int 0x10 + +; however, don't try to access [0x8000] now, because it won't work +; you can only access the stack top so, at this point, only 0x7ffe (look above) +mov al, [0x8000] +int 0x10 + + +; recover our characters using the standard procedure: 'pop' +; We can only pop full words so we need an auxiliary register to manipulate +; the lower byte +pop bx +mov al, bl +int 0x10 ; prints C + +pop bx +mov al, bl +int 0x10 ; prints B + +pop bx +mov al, bl +int 0x10 ; prints A + +; data that has been pop'd from the stack is garbage now +mov al, [0x8000] +int 0x10 + + +jmp $ +times 510-($-$$) db 0 +dw 0xaa55