flat assembler
Message board for the users of flat assembler.

Index > Windows > Test string for null byte '\0'

Author
Thread Post new topic Reply to topic
eeikel



Joined: 17 Jun 2013
Posts: 7
Location: NL
eeikel
Hello, i try to test for a zero byte in a string parsed from c
but somehow the test doesn´t work and i don´t understand why that is

in c:
Code:
char * strzero(const char*);

printf("test asm strzero %s \n",strzero(""));   // should print "zero"
printf("test asm strzero %s \n",strzero("a")); // should print "not zero"
    


Code:
format MS64 COFF
public strzero

       fb_zero db 'zero',0
       fb_not_zero db 'not zero',0
       zero:
                  mov rax, fb_zero
                  pop r10
                  ret
       not_zero:
                  mov rax, fb_not_zero
                  pop r10
                  ret
      strlength:
                 push r10
                 mov r10, rcx
               
                 test r10b, 0x00 ; test low byte for zero
                 je zero;   ; 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
                 jmp not_zero
                 ret   
    
Post 21 Apr 2016, 18:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17341
Location: In your JS exploiting you and your system
revolution
Code:
movzx r10,byte[rcx]    
Or simply:
Code:
cmp byte[rcx],0    
Post 21 Apr 2016, 23:31
View user's profile Send private message Visit poster's website Reply with quote
eeikel



Joined: 17 Jun 2013
Posts: 7
Location: NL
eeikel
thx for the reply,

movzx did'nt work, finally it turns down the test always evaluates to zero Embarassed

anyway i got a string length working as an excersise Very Happy

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               

    
Post 22 Apr 2016, 22:02
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.