C functions in Assembly :O

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
cjgone
Regular Member
Posts: 83
Joined: Tue 18 Apr, 2006 5:33 am
Location: Washington->UC Berkeley '15

C functions in Assembly :O

Post by cjgone »

How do you get the parameters from a C function in assembly and how can you return the value?

The only thing I know is:

Code: Select all

ld hl,2
add hl,sp
For parameters, but i'm not sure....


Code: Select all

char add( char a );
main() {

add( 56 );

}

char add ( char a ) {

//    How do I get parameter?

a = a + 32;

return a;  // how would I return it in assembly.

}
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

How about disassembling the compiled C-code?
why are you using C anyway?
cjgone
Regular Member
Posts: 83
Joined: Tue 18 Apr, 2006 5:33 am
Location: Washington->UC Berkeley '15

Post by cjgone »

I think I figured it out with some tests. :D


How about disassembling the compiled C-code?
why are you using C anyway?
Did, but it has call statements in practically every 5 lines to some other header file which is to confusing for me. :D


Dunno.

Yay, exciting, I found how to get the parameters. :D

Code: Select all

void putspriteXOR(  char x , char height,char y ,char *sprite)
{

#asm
    ld hl,2
    add hl,sp
    ld a,(hl)
    ld e,a
    inc hl
    ld a,(hl)
    ld d,a
    push de
    pop ix
    inc hl
    ld a,(hl)
    ld c,a
    inc hl
    inc hl
    ld a,(hl)
    ld b,a
    inc hl
    inc hl
    ld a,(hl)
    ld l,c
    ; A = x coordinate
; L = y coordinate
; B = number of rows
; IX = address of sprite
    LD     H, 0
    LD     D, H
    LD     E, L
    ADD    HL, HL
    ADD    HL, DE
    ADD    HL, HL
    ADD    HL, HL

    LD     E, A
    SRL    E
    SRL    E
    SRL    E
    ADD    HL, DE

    LD     DE, $9340
    ADD    HL, DE
    AND    7
    JR     Z, _Aligned
    LD     C, A
    LD     DE, 12
._RowLoop
    PUSH   BC
    LD     B, C
    defb $DD, $4E, $00
    XOR    A

._ShiftLoop
    SRL    C
    RRA
    djnz _ShiftLoop
    INC    HL
    XOR    (HL)
    LD     (HL), A

    DEC    HL
    LD     A, C
    XOR    (HL)
    LD     (HL), A

    ADD    HL, DE
    INC    IX
    POP    BC
    djnz _RowLoop
    jr _End

._Aligned
    LD     DE, 12

._PutLoop
    defb $DD, $7E, $00
    XOR    (HL)
    LD     (HL), A
    INC    IX
    ADD    HL, DE
    DJNZ   _PutLoop
._End
#endasm

}
Now i need to find how to return values. :D
erm, I think I found out but i'm unsure...
it seems...

integers are returned in hl

and characters are returned in 'a' with a call statement l_sxt.... or hl


hl ftw :D

-.-
Post Reply