Alright found a few minutes after work here, don't have a whole lot of time during the week. I'm going to keep a running log of the investigation here until I collect it all. Maybe I can write it all up when done.
Compiling the following code with WDC816CC
Code:
void test()
{
const char * buf = "hello world!\r\n";
console_write(buf);
}
this code assumes that console_write is defined elsewhere. It will in fact be in ASM in a later post. Compiled using:
Code:
wdc816cc.exe -a test.c
produces the following code, comments are mine. If I understood anything wrong feel free to correct me.
Code:
;:ts=8
R0 equ 1
R1 equ 5
R2 equ 9
R3 equ 13
code
xdef __test
func
__test:
longa on ; clearly the compiler is assuming we are running A, X and Y as 16 bits.
longi on ; clearly the compiler is assuming we are running A, X and Y as 16 bits.
tsc ; Transfer Stack Pointer to 16-bit Accumulator
sec ; set carry flag -- WHY?
sbc #L2 ; subtract L2 = 2 from A store result in A -- create two bytes of storage space on stack, remember stack GROWS DOWN!
tcs ; Transfer 16-bit Accumulator to Stack Pointer
phd ; Push Direct Page Register to stack -- keep a copy of the old direct page.
tcd ; Transfer 16-bit Accumulator to Direct Page Register -- point the direct page pointer to the stack FAST ACCESS to variables
buf_1 set 0
lda #<L1 ; load address of string
sta <L3+buf_1 ; store the address at L3 + buf_1.
pei <L3+buf_1 ; Push Effective Indirect Addres, aka value in DirectPage 1 as an address pushed onto Stack - the address of the string... Couldn't this be done easier? PEA #L1
; looks like the string is now on the stack.
; for the function it will actually be the 2nd piece on the stack since
; a call will push the return address L4 on the stack.
jsr __console_write
; Functions called by a C function and C functions themselves return values in the X
; register and the Accumulator. The high word of the result, if any, is in the X
; register, while the low word is in the Accumulator.
L4:
pld ; Pull Direct Page Register = e.g. restore the DP to what it was at the beginning of the Func.
tsc ; Transfer Stack Pointer to 16-bit Accumulator
clc ; Clear carry
adc #L2 ; add 2
tcs ; Transfer 16-bit Accumulator to Stack Pointer
rts ; Done baby, no return values.
L2 equ 2
L3 equ 1
ends
efunc
data
L1:
db $68,$65,$6C,$6C,$6F,$20,$77,$6F,$72,$6C,$64,$21,$0D,$0A,$00
ends
xref __console_write
end
next the ASM code __console_write and the ASM code that loads this guy.