flat assembler
Message board for the users of flat assembler.
Index
> Windows > Test string for null byte '\0' |
Author |
|
revolution 21 Apr 2016, 23:31
Code: movzx r10,byte[rcx] Code: cmp byte[rcx],0 |
|||
21 Apr 2016, 23:31 |
|
eeikel 22 Apr 2016, 22:02
thx for the reply,
movzx did'nt work, finally it turns down the test always evaluates to zero anyway i got a string length working as an excersise Code: ; C version of function. ; int test(int x){return (((int)x - 0x01010101) & ~(int)x & 0x80808080);} ; int TYPE_SIZE = (sizeof(int) / sizeof(char)); ; int StringLengthFunction(char *s){ ; char * ptr = s; // copy start address. ; if(*ptr != 0x00){ // if first char is not null, check int ; if((((int)ptr) & (TYPE_SIZE-1)) == 0){ // if int has no zero byte. ; for(;!test(*(int*)ptr); ptr += TYPE_SIZE); // check next int for zero byte, if not loop. ; }// found zero byte in int ; for(;*ptr != 0x00;ptr++); // check bytes in int ; } ; return ptr - s; // return string length address s - address pi = length ; }; format MS64 COFF public strlength strlength: xor rax, rax xor rdx, rdx xor rbx, rbx mov rax, [rcx] ; mov rdx, rcx mov r13, 0x0101010101010101 mov r14, 0x8080808080808080 ; and r10, 0x00000000000000ff test al, al jnz .nextint ; ZF flag not set, byte is not equal to zero, check int je / jz = ZF = 1 equal or zero | jne / jnz ZF = 0 not equal not zero ret .nextbyte: test al, al; ; check the next byte for zero. jz .done ; ZF flag set, byte is equal to zero exit shr rax, 8 add rbx, 1 ; we found one char add 1 to rbx to count the find. jmp .nextbyte .nextint: mov r10, [rcx] ; copy rdx to r10 preserve rcx value mov r11, [rcx] ; copy rdx to r11 preserve rcx value mov rax, [rcx] sub r10, r13 ; r10 - 0x0101010101010101 not r11 ; ~r11 and r11, r14 ; r11 & 0x8080808080808080 test r11, r10 ; r11 & r10 == 0x0000000000 if no zero in qword jnz .nextbyte ; ZF flag not set, byte is not equal to zero, wich means there is a zero check the bytes add rbx, 8 ; we found eight chars add 8 to rbx counter add rcx, 8 ; add 8 to the address to check the next 8 bytes value jmp .nextint ; and jump and loop again .done: mov rax, rbx ret |
|||
22 Apr 2016, 22:02 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.