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 pha ; decsp will destroy A (but not X)
23 jsr decsp4 ; Target/source
25 ; Store the pointer into the source slot
38 ; Get length of S (which is still in a/x)
42 ; Calculate strlen(S)+1 (the space needed)
48 ; Save the space we're about to allocate in ptr4
53 ; Allocate memory. _malloc will not use ptr4
57 ; Store the result into the target stack slot
60 sta (sp),y ; Store low byte
64 sta (sp),y ; Store high byte
66 ; Check for a NULL pointer
71 ; Copy the string. memcpy will return the target string which is exactly
72 ; what we need here. It will also drop the allocated stack frame.
75 ldx ptr4+1 ; Load size
76 jmp _memcpy ; Copy string, drop stackframe
78 ; Out of memory, return NULL (A = 0)
82 jmp incsp4 ; Drop stack frame