X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libsrc%2Fcommon%2Fstrdup.s;h=3ab07bda1d69c97149d5cc1d1a4a9ffa7325fb7d;hb=934a78731fe1b7fc8d08814fa2321534c20fbc62;hp=fa2d289c4e9e4c91b0395eb34a3e3ea1f81bd2c8;hpb=1627af5de86030c01468e98b68d8a903c5894def;p=cc65 diff --git a/libsrc/common/strdup.s b/libsrc/common/strdup.s index fa2d289c4..3ab07bda1 100644 --- a/libsrc/common/strdup.s +++ b/libsrc/common/strdup.s @@ -6,12 +6,13 @@ ; Note: The code knowns which zero page locations are used by malloc. ; - .importzp sp, tmp1, ptr4 - .import pushax, decsp4, incsp4 - .import _strlen, _malloc, _memcpy - .export _strdup + .importzp sp, tmp1, ptr4 + .import pushax, decsp4, incsp4 + .import _strlen, _malloc, _memcpy + .export _strdup - .macpack generic + .macpack cpu + .macpack generic _strdup: @@ -19,62 +20,66 @@ _strdup: ; stack frame. To make this somewhat more efficient, create the stackframe ; as needed for the final call to the memcpy function. - jsr decsp4 ; Target/source + pha ; decsp will destroy A (but not X) + jsr decsp4 ; Target/source ; Store the pointer into the source slot - ldy #0 - sta (sp),y - iny - pha - txa - sta (sp),y - pla + ldy #1 + txa + sta (sp),y + pla +.if (.cpu .bitand CPU_ISET_65SC02) + sta (sp) +.else + dey + sta (sp),y +.endif ; Get length of S (which is still in a/x) - jsr _strlen + jsr _strlen ; Calculate strlen(S)+1 (the space needed) - add #1 - bcc @L1 - inx + add #1 + bcc @L1 + inx ; Save the space we're about to allocate in ptr4 -@L1: sta ptr4 - stx ptr4+1 +@L1: sta ptr4 + stx ptr4+1 ; Allocate memory. _malloc will not use ptr4 - jsr _malloc + jsr _malloc ; Store the result into the target stack slot - ldy #2 - sta (sp),y ; Store low byte - sta tmp1 - txa ; Get high byte - iny - sta (sp),y ; Store high byte + ldy #2 + sta (sp),y ; Store low byte + sta tmp1 + txa ; Get high byte + iny + sta (sp),y ; Store high byte ; Check for a NULL pointer - ora tmp1 - beq OutOfMemory + ora tmp1 + beq OutOfMemory ; Copy the string. memcpy will return the target string which is exactly ; what we need here. It will also drop the allocated stack frame. - lda ptr4 - ldx ptr4+1 ; Load size - jmp _memcpy ; Copy string, drop stackframe + lda ptr4 + ldx ptr4+1 ; Load size + jmp _memcpy ; Copy string, drop stackframe ; Out of memory, return NULL (A = 0) OutOfMemory: - tax - jmp incsp4 ; Drop stack frame + tax + jmp incsp4 ; Drop stack frame