c - Saving registers state in COM program -
i disassembled simple dos .com program , there code saves , restores registers values
push ax ; first instruction push cx .... pop cx pop ax mov ax, 0x00 0x4c int 21 // call dos interrupt 21 => end
this similar function prologue , epilogue in c programs. prologues added automatically compiler, , program above written manually in assembler, programmer took full responsibility saving , restoring values in code.
my question happen if unintentionally forgot save registers in program?
and if intentionally replace these instructions nop
in hex editor? lead program crash? , why called function responsible saving outer context on stack? point of view should done somehow in calling function prevent problems if use 3rd party libraries , poorly written code may break program execution.
one problem of making calling function save of working registers before calling function function interrupted (i.e. hardware interrupt) without knowledge. in dos, example, there pesky 54 millisecond timer tick. 18 times per second, hardware interrupt transfer control whatever code executing timer tick handler. happened automatically unless program disabled interrupts.
the timer tick handler save of registers going use, work, , restore registers saved before returning.
sure, interrupt handlers special, why? paucity of registers on 8086 (ax, bx, cx, dx, si, di, flags -- did forget anything? purposely didn't include segment registers), making function save entire state before transferring control means you'd using lot of unnecessary stack space , execution cycles save things because might modified. if called function responsible saving registers uses, , uses ax , cx, can save 2 registers. makes smaller , faster code, , less stack space usage.
when start talking call hierarchies many levels deep, difference between pushing 8 registers rather 2 registers adds pretty quickly.
consider x86-64, 64 general purpose registers. think function should forced save 64 of registers before calling function, when called function uses 2 of them? saving 64 64-bit registers requires 512 bytes of stack space. opposed saving 2 registers requiring 16 bytes.
the primary point of writing things in assembly language these days write faster , smaller code compiler can write. guiding principle don't more work have to. means it's know registers assembly language function using, , save registers on entry , restore them on exit.
Comments
Post a Comment