2 ; Ullrich von Bassewitz, 18.07.2000
4 ; char* __fastcall__ strdup (const char* S);
6 ; Note: The code knowns which zero page locations are used by malloc.
9 .importzp sp, tmp1, ptr4
10 .import pushax, decsp4, incsp4
11 .import _strlen, _malloc, _memcpy
19 ; Since we need some place to store the intermediate results, allocate a
20 ; stack frame. To make this somewhat more efficient, create the stackframe
21 ; as needed for the final call to the memcpy function.
23 pha ; decsp will destroy A (but not X)
24 jsr decsp4 ; Target/source
26 ; Store the pointer into the source slot
32 .if (.cpu .bitand CPU_ISET_65SC02)
39 ; Get length of S (which is still in a/x)
43 ; Calculate strlen(S)+1 (the space needed)
49 ; Save the space we're about to allocate in ptr4
54 ; Allocate memory. _malloc will not use ptr4
58 ; Store the result into the target stack slot
61 sta (sp),y ; Store low byte
65 sta (sp),y ; Store high byte
67 ; Check for a NULL pointer
72 ; Copy the string. memcpy will return the target string which is exactly
73 ; what we need here. It will also drop the allocated stack frame.
76 ldx ptr4+1 ; Load size
77 jmp _memcpy ; Copy string, drop stackframe
79 ; Out of memory, return NULL (A = 0)
83 jmp incsp4 ; Drop stack frame