Home > Software design >  x86 16 bit assembly function argument registers best practices
x86 16 bit assembly function argument registers best practices

Time:11-04

Lately I have been trying to make my own operating system. To do this I need to learn assembly. It's going well, but I have stumbled upon a problem: what registers can I use for function arguments? And is it a good practice?

CodePudding user response:

For 16-bit, you could mimic DOS.

The function number goes in AH, and the rest of the arguments go in any of the other registers including the DS and ES segment registers for far pointers.
Registers are not chosen in any order, but rather selected for what they will contain:

bx    Handle
cx    Count
ds:dx Far pointer
es:bx Far pointer
...

The result is delivered in any suitable registers but mostly in AX together with the carry flag signaling success or not.

CodePudding user response:

what registers can I use for function arguments? And is it a good practice?

The only requirements are that the caller passes parameters in a way that the callee expects, and the callee returns parameters in a way the caller expects. For assembly, every routine can do whatever is convenient for itself, and it's easy to keep track just using some comments, like:

; Routine to juggle the wobbly things
;
;Input
; ax   First thing
; bx   Second thing
;
;Output
; cx   First result
; dx   Second result
;
;Trashed
; ax, bx, bp

juggleTheWobblies:

Note that for 80x86 some instructions have implied registers (mul, div, xlat, loop, stosd, in, ...) which make it easy to determine the best way to pass parameters (to avoid moving stuff from the wrong register into the register the instruction needs).

A specific ABI (like "1st parameter goes in this register, 2nd parameter goes in that register, ..") is only ever needed when your tools (compiler, linker) aren't able to optimize properly. This mostly occurs when you're doing dynamic linking, or static linking without link time code generation. For almost all other cases a compiler could do the same as assembly (pass parameters in whichever way seems optimal for that specific function with no ABI at all).

  • Related