flat assembler
Message board for the users of flat assembler.
Index
> Main > Fast Way to Get String Length (StrLen) Goto page Previous 1, 2 |
Author |
|
packet_50071 02 Jan 2008, 18:14
can some one pls explain me how does the "align 8" string allocations. work ?
|
|||
02 Jan 2008, 18:14 |
|
bitRAKE 03 Jan 2008, 03:38
I like the following way because it's easy to remember 0 and 1, lol.
Code: add eax, alignment-1 and eax, 0-alignment |
|||
03 Jan 2008, 03:38 |
|
Hayden 05 Jan 2008, 07:54
-- Author - Bernard W. Ryan
-- Copyright (c) February 2000 - 2006 -- Verision 7.00 -- -- Last Revision Date January 2004 -- Tested for use with Euphoria ver. 2.5 NOTE: this is a snippet, so some labels may not be declared, this is just to show you guys some nice string routines. Code: ; <SNIP> -- 26: strlen_: @ #60, -- 26: pushad save all the registers #BD,#00,#00,#00,#00, -- 27: mov ebp, stack point base pointer to stack (40) #FC, -- 2C: cld clear the direction flag #8B,#7D,#04, -- 2D: mov edi, [ebp+4] offset of str parameter #31,#C0, -- 30: xor eax, eax set al to contain zero #83,#C9,#FF, -- 32: or ecx, -1 set counter to #FFFFFFFF #F2,#AE, -- 35: repne scasb scan for zero termination #F7,#D1, -- 37: not ecx invert ecx to get the count #49, -- 39: dec ecx subtract 1 from the ecx ( null termination ) #89,#4D,#00, -- 3A: mov [ebp], ecx Return ecx ( the length ) #61, -- 3D: popad restore all the registers -- 3E: ret back to Euphoria #C3, -- 3E: -- 3F: strcpy_: @ #60, -- 3F: pushad save all the registers #BD,#00,#00,#00,#00, -- 40: mov ebp, stack point base pointer to stack (65) #FC, -- 45: cld clear the direction flag #8B,#75,#08, -- 46: mov esi, [ebp+8] get src pointer #89,#F7, -- 49: mov edi, esi save a copy of src pointer in the edi #31,#C0, -- 4B: xor eax, eax set al to contain zero #83,#C9,#FF, -- 4D: or ecx, -1 set counter to #FFFFFFFF #F2,#AE, -- 50: repne scasb scan for zero termination #F7,#D1, -- 52: not ecx invert ecx to get the count #8B,#7D,#04, -- 54: mov edi, [ebp+4] get pointer to dst string #89,#7D,#00, -- 57: mov [ebp], edi return dst pointer #89,#CA, -- 5A: mov edx,ecx save the counter #C1,#E9,#02, -- 5C: shr ecx, 2 divide count by 4 to get number of double words #F3,#A5, -- 5F: rep movsd copy the double words #89,#D1, -- 61: mov ecx,edx get back the count #83,#E1,#03, -- 63: and ecx, 3 mask out the number of bytes left over #F3,#A4, -- 66: rep movsb copy the remainder of bytes #61, -- 68: popad restore all the registers -- 69: ret back to Euphoria #C3, -- 69: -- 6A: stpcpy_: @ #60, -- 6A: pushad save all the registers #BD,#00,#00,#00,#00, -- 6B: mov ebp, stack point base pointer to stack (108) #FC, -- 70: cld clear the direction flag #8B,#75,#08, -- 71: mov esi, [ebp+8] get pointer to src string #89,#F7, -- 74: mov edi, esi get a copy of src in the edi #83,#C9,#FF, -- 76: or ecx, -1 set counter to #FFFFFFFF #31,#C0, -- 79: xor eax, eax set al to contain zero #F2,#AE, -- 7B: repne scasb calculate the src length #F7,#D1, -- 7D: not ecx invert ecx to get the count #8B,#7D,#04, -- 7F: mov edi, [ebp+4] get pointer to dst string #89,#CA, -- 82: mov edx,ecx save the counter #C1,#E9,#02, -- 84: shr ecx, 2 divide count by 4 to get number of double words #F3,#A5, -- 87: rep movsd copy the double words #89,#D1, -- 89: mov ecx,edx get back the count #83,#E1,#03, -- 8B: and ecx, 3 mask out the number of bytes left over #F3,#A4, -- 8E: rep movsb copy the remainder of bytes #8D,#47,#FF, -- 90: lea eax, [edi-1] adjust edi to point to zero terminating byte #89,#45,#00, -- 93: mov [ebp], eax setup to return edi pointer #61, -- 96: popad restore all the registers -- 97: ret back to Euphoria #C3, -- 97: -- 98: strupr_: @ #60, -- 98: pushad save all the registers #BD,#00,#00,#00,#00, -- 99: mov ebp, stack point base pointer to stack (154) #8B,#7D,#04, -- 9E: mov edi, [ebp+4] get pointer to the string #89,#7D,#00, -- A1: mov [ebp], edi setup return pointer to processed string -- A4: testcase1: look for uppercase #8B,#07, -- A4: mov eax, [edi] get character #3C,#61, -- A6: cmp al, 97 is character below 'a' ? #72,#08, -- A8: jb next1 yes, already uppercase goto next #3C,#7A, -- AA: cmp al, 122 is character above 'z' ? #77,#04, -- AC: ja next1 yes, already uppercase goto next #24,#5F, -- AE: and al, 95 mask out bit 5 using 5Fh as mask #88,#07, -- B0: mov [edi], al replace character with uppercase character -- B2: next1: the character is now upper case #47, -- B2: inc edi point to next character #08,#C0, -- B3: or al, al test al for zero #75,#ED, -- B5: jnz testcase1 jump to test case unless zero #61, -- B7: popad restore all the registers -- B8: ret back to Euphoria #C3, -- B8: -- B9: strlwr_: @ #60, -- B9: pushad save all the registers #BD,#00,#00,#00,#00, -- BA: mov ebp, stack point base pointer to stack (187) #8B,#7D,#04, -- BF: mov edi, [ebp+4] get pointer to string #89,#7D,#00, -- C2: mov [ebp], edi setup return pointer to processed string -- C5: testcase2: look for lowercase #8B,#07, -- C5: mov eax, [edi] get character #3C,#41, -- C7: cmp al, 65 is character below 'A' ? #72,#08, -- C9: jb next2 yes, already lowercase goto next #3C,#5A, -- CB: cmp al, 90 is character above 'Z' ? #77,#04, -- CD: ja next2 yes, already lowercase goto next #0C,#20, -- CF: or al, 32 or bit 5 into byte ( using 20h ) #88,#07, -- D1: mov [edi], al replace character with lowercase character -- D3: next2: the character is now lower case #47, -- D3: inc edi point to next character #08,#C0, -- D4: or al, al test al for zero #75,#ED, -- D6: jnz testcase2 jump to test case unless zero #61, -- D8: popad restore all the registers -- D9: ret back to Euphoria #C3, -- D9: -- DA: strcat_: @ #60, -- DA: pushad save all the registers #BD,#00,#00,#00,#00, -- DB: mov ebp, stack point base pointer to stack (220) #FC, -- E0: cld clear the direction flag #8B,#7D,#04, -- E1: mov edi, [ebp+4] get pointer to dst string #89,#7D,#00, -- E4: mov [ebp], edi setup to return dst pointer #8B,#75,#08, -- E7: mov esi, [ebp+8] get pointer to src string #83,#C9,#FF, -- EA: or ecx, -1 set counter to #FFFFFFFF #31,#C0, -- ED: xor eax, eax set al to contain zero #F2,#AE, -- EF: repne scasb find end of string ( null ) #4F, -- F1: dec edi backup over null terminator #AC, -- F2: next3: lodsb load a byte #AA, -- F3: stosb store a byte #3C,#00, -- F4: cmp al, 0 check for end of string #75,#FA, -- F6: jnz next3 no, get next byte #61, -- F8: popad restore all the registers -- F9: ret back to Euphoria #C3, -- F9: -- FA: strchr_: @ #60, -- FA: pushad save all the registers #BD,#00,#00,#00,#00, -- FB: mov ebp, stack point base pointer to stack (252) #FC, -- 100: cld clear the direction flag #8B,#75,#04, -- 101: mov esi, [ebp+4] get pointer of string to search #8A,#65,#08, -- 104: mov ah, [ebp+8] get character to look for -- 107: look4: start looking #AC, -- 107: lodsb load a byte into al #38,#C4, -- 108: cmp ah, al compare, is this the character ? #74,#08, -- 10A: jz found4 if yes - jump to found #3C,#00, -- 10C: cmp al, 0 is end of string ? #75,#F7, -- 10E: jne look4 if no - look at next chracter #31,#C0, -- 110: xor eax, eax not found - zero eax ( a null ) #EB,#03, -- 112: jmp short exit4 clean up and exit -- 114: found4: yes, found #89,#F0, -- 114: mov eax, esi get the pointer to character #48, -- 116: dec eax correct the pointer for lodsb increment -- 117: exit4: exit #89,#45,#00, -- 117: mov [ebp], eax return the pointer to character or a null #61, -- 11A: popad restore all the registers -- 11B: ret back to Euphoria #C3, -- 11B: -- 11C: strcmp_: @ #60, -- 11C: pushad save the registers #BD,#00,#00,#00,#00, -- 11D: mov ebp, stack point base pointer to stack (286) #FC, -- 122: cld clear the direction flag #8B,#7D,#08, -- 123: mov edi, [ebp+8] get pointer to str2 string #89,#FE, -- 126: mov esi,edi save a copy of str2 pointer #83,#C9,#FF, -- 128: or ecx,-1 set counter to #FFFFFFFF #31,#C0, -- 12B: xor eax,eax set al to contain zero #F2,#AE, -- 12D: repne scasb calculate the str2 length #F7,#D1, -- 12F: not ecx invert ecx to get the count #89,#F7, -- 131: mov edi,esi get saved copy of str2 #8B,#75,#04, -- 133: mov esi, [ebp+4] get pointer to str1 #F3,#A6, -- 136: repe cmpsb while str1 = str2 loop #0F,#9F,#C0, -- 138: setg al set the al to 1 if greater #0F,#9C,#C4, -- 13B: setl ah set the ah to 1 if less #28,#E0, -- 13E: sub al,ah subtract the ah from the al #0F,#BE,#C0, -- 140: movsx eax,al set eax to reflect ordering #89,#45,#00, -- 143: mov [ebp], eax return 1 or -1 or zero #61, -- 146: popad restore registers -- 147: ret back to Euphoria #C3, -- 147: -- 148: strcspn_: @ #60, -- 148: pushad save the registers #BD,#00,#00,#00,#00, -- 149: mov ebp, stack point base pointer to stack (330) #FC, -- 14E: cld clear the direction flag #31,#C0, -- 14F: xor eax, eax clear the counter #8B,#4D,#04, -- 151: mov ecx, [ebp+4] get pointer to str1 string #EB,#12, -- 154: jmp short eos5 go test for end of string -- 156: nextptr5: #41, -- 156: inc ecx point to next character in str1 #8B,#7D,#08, -- 157: mov edi, [ebp+8] get pointer to str2 string -- 15A: scan5: #8A,#37, -- 15A: mov dh, [edi] store character from str2 in dh #08,#F6, -- 15C: or dh, dh test for zero termination #74,#07, -- 15E: jz inceos5 yes, go an update the counter #38,#F2, -- 160: cmp dl, dh compare str1 and str2 characters #74,#0A, -- 162: je exit5 if equal exit #47, -- 164: inc edi point to next str2 character #EB,#F3, -- 165: jmp short scan5 jump to scan next -- 167: inceos5: #40, -- 167: inc eax update the counter -- 168: eos5: #8A,#11, -- 168: mov dl, [ecx] get next character #08,#D2, -- 16A: or dl, dl test for zero termination #75,#E8, -- 16C: jnz nextptr5 no, goto next pointer -- 16E: exit5: #89,#45,#00, -- 16E: mov [ebp], eax return the index counter #61, -- 171: popad restore all the registers -- 172: ret back to Euphoria #C3, -- 172: -- 173: strspn_: @ #60, -- 173: pushad save the registers #BD,#00,#00,#00,#00, -- 174: mov ebp, stack point base pointer to stack (373) #FC, -- 179: cld clear the direction flag #31,#C0, -- 17A: xor eax, eax clear the counter #8B,#4D,#04, -- 17C: mov ecx, [ebp+4] get pointer to str1 string #EB,#12, -- 17F: jmp short eos6 test for end of string -- 181: nextptr6: next pointer #41, -- 181: inc ecx point to next character in str1 #8B,#7D,#08, -- 182: mov edi, [ebp+8] get pointer to str2 string -- 185: scan6 scan for character #8A,#37, -- 185: mov dh, [edi] store character from str2 in dh #08,#F6, -- 187: or dh, dh test for zero termination #74,#0E, -- 189: jz exit6 if equal exit #38,#F2, -- 18B: cmp dl, dh compare str1 and str2 characters #74,#03, -- 18D: je update6 yes, go an update the counter #47, -- 18F: inc edi point to next str2 character #EB,#F3, -- 190: jmp short scan6 jump to scan next -- 192: update6: update #40, -- 192: inc eax update the counter -- 193: eos6: look for the end of the string #8A,#11, -- 193: mov dl, [ecx] get next character #08,#D2, -- 195: or dl, dl test for zero termination #75,#E8, -- 197: jnz nextptr6 no, goto next pointer -- 199: exit6: exit #89,#45,#00, -- 199: mov [ebp], eax return the index counter #61, -- 19C: popad restore all the registers -- 19D: ret back to Euphoria #C3, -- 19D: -- 19E: strncat_: @ #60, -- 19E: pushad save all the registers #BD,#00,#00,#00,#00, -- 19F: mov ebp, stack point base pointer to stack (416) #FC, -- 1A4: cld clear the direction flag #8B,#7D,#04, -- 1A5: mov edi, [ebp+4] get pointer to dst string #89,#7D,#00, -- 1A8: mov [ebp], edi setup to return dst pointer #8B,#75,#08, -- 1AB: mov esi, [ebp+8] get pointer to src string #8B,#5D,#0C, -- 1AE: mov ebx, [ebp+12] get count #83,#C9,#FF, -- 1B1: or ecx, -1 set counter to #FFFFFFFF #31,#C0, -- 1B4: xor eax, eax set al to contain zero #F2,#AE, -- 1B6: repne scasb find end of string ( null ) #4F, -- 1B8: dec edi back up to overwrite terminating null -- 1B9: next7: next character #83,#FB,#00, -- 1B9: cmp ebx, 0 is count in ebx 0 ? #74,#07, -- 1BC: jz exit7 yes exit #AC, -- 1BE: lodsb get a character #AA, -- 1BF: stosb store the character #4B, -- 1C0: dec ebx decrement ebx count #3C,#00, -- 1C1: cmp al, 0 test for end of string #75,#F4, -- 1C3: jnz next7 no get next character -- 1C5: exit7: exit #31,#C0, -- 1C5: xor eax, eax get a zero #89,#1F, -- 1C7: mov [edi], ebx store the zero termination byte end of string #61, -- 1C9: popad restore all the registers -- 1CA: ret back to Euphoria #C3, -- 1CA: -- 1CB: strncmp_: @ #60, -- 1CB: pushad save the registers #BD,#00,#00,#00,#00, -- 1CC: mov ebp, stack point base pointer to stack (461) #FC, -- 1D1: cld clear the direction flag #8B,#7D,#08, -- 1D2: mov edi, [ebp+8] get pointer to str2 string #89,#FE, -- 1D5: mov esi,edi save a copy of str2 pointer #83,#C9,#FF, -- 1D7: or ecx,-1 set counter to #FFFFFFFF #31,#C0, -- 1DA: xor eax,eax set al to contain zero #F2,#AE, -- 1DC: repne scasb calculate the src length #F7,#D1, -- 1DE: not ecx invert ecx to get the count #8B,#55,#0C, -- 1E0: mov edx, [ebp+12] ; ; get count #39,#D1, -- 1E3: cmp ecx,edx have we reach the count ? #76,#02, -- 1E5: jbe short -- 1E7: over8 jump over if below or equal #89,#D1, -- 1E7: mov ecx,edx save the count -- 1E9: jecxz short exit8 exit if ecx is zero -- 1E9: over8: below or equal #89,#F7, -- 1E9: mov edi,esi get saved copy of str2 #8B,#75,#04, -- 1EB: mov esi, [ebp+4] get the count into edx #F3,#A6, -- 1EE: repe cmpsb while str1 = str2 loop #0F,#9F,#C0, -- 1F0: setg al set the al to 1 if greater #0F,#9C,#C4, -- 1F3: setl ah set the ah to 1 if less #28,#E0, -- 1F6: sub al,ah subtract the ah from the al #0F,#BE,#C0, -- 1F8: movsx eax,al set eax to reflect ordering -- 1FB: exit8: exit #89,#45,#00, -- 1FB: mov [ebp], eax return 1 or -1 or zero #61, -- 1FE: popad restore registers -- 1FF: ret back to Euphoria #C3, -- 1FF: -- 200: strncpy_: @ #60, -- 200: pushad save all the registers #BD,#00,#00,#00,#00, -- 201: mov ebp, stack point base pointer to stack (514) #FC, -- 206: cld clear the direction flag #8B,#55,#0C, -- 207: mov edx, [ebp+12] get the count #8B,#75,#08, -- 20A: mov esi, [ebp+8] get pointer to src string #89,#F7, -- 20D: mov edi, esi get a copy of src in the edi #31,#C0, -- 20F: xor eax, eax set al to contain zero #83,#C9,#FF, -- 211: or ecx, -1 set counter to #FFFFFFFF #F2,#AE, -- 214: repne scasb calculate the src length #F7,#D1, -- 216: not ecx invert ecx to get the count #89,#C8, -- 218: mov eax, ecx save the src length #89,#D1, -- 21A: mov ecx, edx get number of characters to copy #8B,#7D,#04, -- 21C: mov edi, [ebp+4] get pointer to dst string #89,#7D,#00, -- 21F: mov [ebp], edi get pointer to dst string #F3,#A4, -- 222: repz movsb move bytes from src to dst #89,#D1, -- 224: mov ecx, edx get src length #29,#C1, -- 226: sub ecx, eax cnt - src = number of pad bytes #7F,#06, -- 228: jg exit9 exit no pad bytes #F7,#D1, -- 22A: not ecx invert the ecx #31,#C0, -- 22C: xor eax, eax get null pad character #F3,#AA, -- 22E: repz stosb fill with pad bytes -- 230: exit9: #61, -- 230: popad restore all the registers -- 231: ret back to Euphoria #C3, -- 231: -- 232: strpbrk_: @ #60, -- 232: pushad save all the registers #BD,#00,#00,#00,#00, -- 233: mov ebp, stack point base pointer to stack (564) #FC, -- 238: cld clear the direction flag #8B,#45,#04, -- 239: mov eax, [ebp+4] get the count #8B,#5D,#08, -- 23C: mov ebx, [ebp+8] get the count #EB,#10, -- 23F: jmp short nextstr1 - get string1 1st character -- 241: getstr2: string2 pointer #89,#D9, -- 241: mov ecx,ebx get head of str2 pointer -- 243: nextstr2: next character from string 2 #8A,#31, -- 243: mov dh,[ecx] get str2 char into the dh #08,#F6, -- 245: or dh,dh test for end of string #74,#07, -- 247: jz incstr1 yes, #38,#F2, -- 249: cmp dl,dh compare str1 with str2 #74,#0C, -- 24B: je exit10 if a match exit #41, -- 24D: inc ecx increment str2 pointer #EB,#F3, -- 24E: jmp short nextstr2 - get next -- 250: incstr1: update str1 pointer #40, -- 250: inc eax increment str1 pointer -- 251: nextstr1: next character from string 1 #8A,#10, -- 251: mov dl,[eax] get str1 char into the dl #08,#D2, -- 253: or dl,dl test for end of string #75,#EA, -- 255: jnz getstr2 no, get new str2 pointer #31,#C0, -- 257: xor eax,eax setup eax to return ZERO -- 259: exit10: exit #89,#45,#00, -- 259: mov [ebp], eax return a pointer or null #61, -- 25C: popad restore all the registers -- 25D: ret back to Euphoria #C3, -- 25D: -- 25E: strrchr_: @ #60, -- 25E: pushad save all the registers #BD,#00,#00,#00,#00, -- 25F: mov ebp, stack point base pointer to stack (608) #FC, -- 264: cld clear the direction flag #8B,#75,#04, -- 265: mov esi, [ebp+4] get pointer of string to search #8A,#65,#08, -- 268: mov ah, [ebp+8] get character to look for in ebx #31,#DB, -- 26B: xor ebx, ebx set ebx counter to contain zero -- 26D: look11: look #AC, -- 26D: lodsb load a byte into al #38,#C4, -- 26E: cmp ah, al compare is this the character ? #74,#06, -- 270: jz found11 if yes - jump to found #3C,#00, -- 272: cmp al, 0 is end of string ? #75,#F7, -- 274: jne look11 if no - look at next chracter #EB,#05, -- 276: jmp short exit11 clean up and exit -- 278: found11: found #89,#F3, -- 278: mov ebx, esi get the pointer #4B, -- 27A: dec ebx correct the pointer #EB,#F0, -- 27B: jmp short look11 look for next occurance -- 27D: exit11: exit #89,#5D,#00, -- 27D: mov [ebp], ebx return a pointer or zero #61, -- 280: popad restore all the registers -- 281: ret back to Euphoria #C3, -- 281: -- 282: strset_: @ #60, -- 282: pushad save all the registers #BD,#00,#00,#00,#00, -- 283: mov ebp, stack point base pointer to stack (644) #FC, -- 288: cld clear the direction flag #8B,#7D,#04, -- 289: mov edi, [ebp+4] get pointer of string to search #89,#7D,#00, -- 28C: mov [ebp], edi setup return pointer to processed string #B4,#00, -- 28F: mov ah, 0 zero terminating byte #8A,#45,#08, -- 291: mov al, [ebp+8] get character to look for in al #EB,#01, -- 294: jmp short eos11 test end of string #AA, -- 296: set11: stosb set the character #3A,#27, -- 297: eos11: cmp ah, [edi] -- end of string ? #75,#FB, -- 299: jnz set11 no set the next character #61, -- 29B: popad restore all the registers -- 29C: ret back to Euphoria #C3}) -- 29C: ; <SNIP> _________________ New User.. Hayden McKay. |
|||
05 Jan 2008, 07:54 |
|
Goto page Previous 1, 2 < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.