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
18 ; Since we need some place to store the intermediate results, allocate a
19 ; stack frame. To make this somewhat more efficient, create the stackframe
20 ; as needed for the final call to the memcpy function.
22 jsr decsp4 ; Target/source
24 ; Store the pointer into the source slot
34 ; Get length of S (which is still in a/x)
38 ; Calculate strlen(S)+1 (the space needed)
44 ; Save the space we're about to allocate in ptr4
49 ; Allocate memory. _malloc will not use ptr4
53 ; Store the result into the target stack slot
56 sta (sp),y ; Store low byte
60 sta (sp),y ; Store high byte
62 ; Check for a NULL pointer
67 ; Copy the string. memcpy will return the target string which is exactly
68 ; what we need here. It will also drop the allocated stack frame.
71 ldx ptr4+1 ; Load size
72 jmp _memcpy ; Copy string, drop stackframe
74 ; Out of memory, return NULL (A = 0)
78 jmp incsp4 ; Drop stack frame