Skip to content

[msdos][question] Which registers to preserve when calling an Assembly routine from C? #347

@Fabrizio-Caruso

Description

@Fabrizio-Caruso

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions