You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[msdos][question] Which registers to preserve when calling an Assembly routine from C? #347
I have managed to write a simple plot routine to plot in VGA, which seems to work perfectly (so far).
I have no previous experience with Intel Assembly. I wonder whether I should preserve all registers or if ACK does automatically preserves some registers or if it avoids using some registers so that there is no need to preserve them.
My code looks like this:
.define _plot
_plot:
push bp
mov bp, sp
push ax
push bx
push cx
push dx
push di
! mov bx, sp
mov cx, 4(bp)
mov dx, 6(bp)
! Set ES = A000h (VGA memory)
mov ax, 0xA000
mov es, ax
! Compute offset = y*320 + x
mov ax, dx ! AX = y
mov bx, 320
mul bx ! AX = y*320 (fits in 16 bits)
add ax, cx ! AX = y*320 + x
mov di, ax ! DI = offset
movb al, 8(bp) ! color
! Write pixel using STOSB
stosb ! [ES:DI] = AL, increments DI
pop di
pop dx
pop cx
pop bx
pop ax
pop bp
ret
with this prototype
extern void plot(uint16_t x, uint16_t y, uint8_t color);
Should I avoid some of the push/pop instructions at the beginning and at the end of the routine?
It's just esi and edi (and ebp and the segment registers, of course), as far as I can tell, and that only if register variables are being used. You don't need to save anything else.
Activity
davidgiven commentedon Nov 27, 2025
It's just esi and edi (and ebp and the segment registers, of course), as far as I can tell, and that only if register variables are being used. You don't need to save anything else.
Fabrizio-Caruso commentedon Nov 29, 2025
I am not so sure. Maybe it was my buggy code. I had to preserve CX to get a routine not to misbehave. Maybe I am wrong.
tkchia commentedon Jan 5, 2026
Hello @Fabrizio-Caruso ,
I think you might have forgotten to preserve
es? Thank you!