Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Calling Conventions parameters are passed on the run-time or system stack, SP (or A7) parameters pushed on stack in “right to left” order of call A6.

Similar presentations


Presentation on theme: "C Calling Conventions parameters are passed on the run-time or system stack, SP (or A7) parameters pushed on stack in “right to left” order of call A6."— Presentation transcript:

1 C Calling Conventions parameters are passed on the run-time or system stack, SP (or A7) parameters pushed on stack in “right to left” order of call A6 used as the stack frame pointer local variables located at a negative offset to the frame pointer in “left to right” order of declaration parameters located at a positive offset to the frame pointer return values via registers: D0 for values A0 for pointer

2 C Calling Conventions int CallingFunction (int x) { int y;
CalledFunction(1,2); return (5); } void CalledFunction (int p1, int p2) int local1, local2; local1 = p2; Adapted from C to Assembly Translation [ © 2014 EventHelix.com Inc.] int CallingFunction (int x) { LINK A6, #-4 MOVE.L #2, -(SP) MOVE.L #1, -(SP) JSR CalledFunction ADDQ.L #8, SP MOVEQ.L #5, D0 UNLK A6 RTS } void CalledFunction (int p1, int p2) LINK A6, #-8 MOVE.L 12(A6), -4(A6)

3 Calling APIs … example PalmOS
API = Application Program Interface the APIs themselves may be written in C or assembly and the APIs may be called from C or assembly routines, but the C calling convention is always used in the API library, find the routine you want to use eg. use StrLen to find the length of a string in the assembly program issue an operating system call PalmOS syntax is ASM_SYS_TRAP(sysTrapRoutineName) eg. ASM_SYS_TRAP(sysTrapStrLen) ASM_SYS_TRAP(sysTrapStrLen) will be assembled into TRAP #15  <- operating system trap DC.L  A0C7  <- PalmOS core trap number for StrLen Palm OS Core Trap Numbers [CodeWarrior® Development Tools; CoreTraps.h]

4 asm WriteString() { MOVEM.L A0-A2/A5/D0-D3, -(SP) MOVEA.L A0, A2 MOVE.L A2, -(SP) ASM_SYS_TRAP (sysTrapStrLen) MOVE.L D0, D3 ADDQ.L #4, SP JSR GetConsole MOVE.W D3, -(SP) MOVE.L A0, -(SP) ASM_SYS_TRAP (sysTrapFldInsert) ADDA.L #10, SP MOVEM.L (SP)+, A0-A2/A5/D0-D3 RTS } The routine as it would be written for the PalmOS. StrLen Purpose Compute the length of a string. Declared In StringMgr.h Prototype UInt16 StrLen (const Char *src) Parameters → src Pointer to a string. Returns Returns the length of the string in bytes. Comments Use this function instead of the standard strlen routine. This function returns the length of the string in bytes. On systems that support multi-byte characters, the number returned does not always equal the number of characters.

5 MOVEM.L A0-A2/A5/D0-D3, -(SP) MOVEA.L A0, A2 MOVE.L A2, -(SP)
FldInsert Purpose Replace the current selection with the specified string; redraw the field. Prototype Boolean FldInsert ( FieldType *fldP, const Char *insertChars, UInt16 insertLen) Parameters → fldP Pointer to the field object (FieldType structure) to insert to. → insertChars Text string to be inserted. → insertLen Length in bytes of the text string to be inserted, not counting the trailing null character. Returns true if string successfully inserted. false if: The insertLen parameter is 0. The field is not editable. Adding the text would exceed the field's size limit (the maxChars value). More memory must be allocated for the field, and the allocation fails. asm WriteString() { MOVEM.L A0-A2/A5/D0-D3, -(SP) MOVEA.L A0, A2 MOVE.L A2, -(SP) ASM_SYS_TRAP (sysTrapStrLen) MOVE.L D0, D3 ADDQ.L #4, SP JSR GetConsole MOVE.W D3, -(SP) MOVE.L A0, -(SP) ASM_SYS_TRAP (sysTrapFldInsert) ADDA.L #10, SP MOVEM.L (SP)+, A0-A2/A5/D0-D3 RTS }

6 asm WriteString() { MOVEM.L A0-A2/A5/D0-D3, -(SP) MOVEA.L A0, A2 MOVE.L A2, -(SP) ASM_SYS_TRAP (sysTrapStrLen) MOVE.L D0, D3 ADDQ.L #4, SP JSR GetConsole MOVE.W D3, -(SP) MOVE.L A0, -(SP) ASM_SYS_TRAP (sysTrapFldInsert) ADDA.L #10, SP MOVEM.L (SP)+, A0-A2/A5/D0-D3 RTS } What happens on the stack:

7 #define SHIFT_COUNT 16(a7) Note: a7 ≡ sp
From longlong68K.c /* Purpose..: Compute result = left << right */ extern asm CInt64* __rt_shl64(CInt64 *result, CInt64 left, short count) { move.l LEFT_LO,d0 move.l LEFT_HI,d1 move.w SHIFT_COUNT,d2 and.w #0x003F,d2 bra.s l1 l0: add.l d0,d0 addx.l d1,d1 l1: dbf d2,l0 move.l RESULT,a0 move.l d0,RESULT_LO move.l d1,RESULT_HI RETURN } #define RETURN rts #define RETURNINT rts #define RESULT (a7) #define RESULT_HI (a0) #define RESULT_LO 4(a0) #define LEFT_HI (a7) #define LEFT_LO (a7) #define SHIFT_COUNT 16(a7) Note: a7 ≡ sp

8 From longlong68K.c /* Purpose..: Compute result = left << right */ extern asm CInt64* __rt_shl64(CInt64 *result, CInt64 left, short count) { move.l 12(sp),d0 move.l 8(sp),d1 move.w 16(sp),d2 and.w #0x003F,d2 bra l1 l0: add.l d0,d0 addx.l d1,d1 l1: dbf d2,l0 move.l 4(sp),a0 move.l d0,4(a0) move.l d1,(a0) rts }

9 From longlong68K.c /*Purpose..: Compare left and right (unsigned) */ extern asm short_rt_cmps64(CInt64 left,CInt64 right) { move.l -4+LEFT_LO,d2 move.l -4+LEFT_HI,d0 move.l -4+RIGHT_HI,d1 sub.l -4+RIGHT_LO,d2 subx.l d1,d0 beq.s eq blt.s lt gt: moveq #1,d0 RETURNINT lt: moveq #-1,d0 eq: RETURNINT } { move.l -4+12(a7),d2 move.l -4+8(a7),d0 move.l -4+16(a7),d1 sub.l -4+20(a7),d2 subx.l d1,d0 beq.s eq blt.s lt gt: moveq #1,d0 rts lt: moveq #-1,d0 eq: rts }

10 From longlong68K.c /*Purpose..: Compare left and right (unsigned) */ extern asm short_rt_cmpu64(CInt64 left,CInt64 right) { move.l -4+LEFT_HI,d0 cmp.l -4+RIGHT_HI,d0 beq.s lo bcs.s lt gt: moveq #1,d0 RETURNINT lo: move.l -4+LEFT_LO,d0 cmp.l -4+RIGHT_LO,d0 beq.s eq bcc.s gt lt: moveq #-1,d0 eq: moveq #0,d0 } { move.l -4+8(a7),d0 cmp.l -4+16(a7),d0 beq.s lo bcs.s lt gt: moveq #1,d0 rts lo: move.l -4+12(a7),d0 cmp.l -4+20(a7),d0 beq.s eq bcc.s gt lt: moveq #-1,d0 eq: moveq #0,d0 }

11 Reading: Expectations:
C to assembly: function calling [ © 2014 EventHelix.com Inc.] - explains function calls and frame pointers using a pseudo assembler - draws stack in opposite direction (high address at top) from our convention eg. see WriteString, WriteNum, ClearScreen, etc from cp216Lib.c (old set of routines used in the Palm) ; uses PalmOS API routines (see reference below) Palm OS API examples at: Palm OS® Programmer’s API Reference [pdf, 2670p, © , PalmSource, Inc.] eg. the PalmOS 64 bit arithmetic library, LongLong68K.c - good examples of parameter passing, does not use stack frames Although not directly relevant to calling C routines, you should now be able to grasp the details of the explanation: C to assembly: loops, structs and arrays [© 2014 EventHelix.com Inc.] C to assembly: if and switch statements [© 2014 EventHelix.com Inc.] depending on the processor you are running on, the assembly language will change but the principles of the C calling sequence and the stack frame stay the same. See Mixing Assembly and C [Edited by Peter L. B. Johnson, University of Illinois at Urbana-Champaign, 2004 ] which uses Intel assembly language. Even though you know no Intel assembly language, you should be able to figure out what is going on. Expectations: you are responsible for all material in the reading you should be able to correctly call a C routine from assembly language when given the prototype and parameter descriptions


Download ppt "C Calling Conventions parameters are passed on the run-time or system stack, SP (or A7) parameters pushed on stack in “right to left” order of call A6."

Similar presentations


Ads by Google