string - Finding escape characters in AT&T x86 assembly -


question 1: have following assembler code, purpose loop through input string, , count number of escape characters '%' encounters:

.globl sprinter  .data  .escape_string: .string "%"  .num_escape: .long 0  .num_characters: .long 0  .text  sprinter:     pushl %ebp     movl %esp,%ebp      movl 8(%ebp),%ecx # %ecx = parameter 1  loop:     cmpb $0, (%ecx) # if end of string reached     jz exit     cmpl $.escape_string,(%ecx) # if escape character found     je increment     back:         incl .num_characters         incl %ecx         jmp loop  increment:     incl .num_escape     jmp # jump 'back'  exit:     movl .num_escape, %eax # return num_escape      popl %ebp     ret 

this assembly code compiled following c code:

#include <stdio.h> extern int sprinter (char* string); int main (void) {     int n = sprinter("a %d string of %s fashion!");     printf("value: %d",n);     return 0; } 

the expected output running code value: 2 (because there 2 '%' characters in string), returns value: 0, meaning following line fails (because never increments counter):

cmpl $.escape_string,(%ecx) # if escape character found 

am using wrong method of comparing string? outer loop works fine, , .num_characters correctly contains number of characters in string. generated assembly code simple c-program compared string "hello" "hello2", , relevant code:

.lc0:     .string "hello" .lc1:     .string "hello2"  ... movl    $.lc0, -4(%ebp) cmpl    $.lc1, -4(%ebp) 

it looks similar tried, no?

question 2. code part of going simplified sprintf-function written in assembly. means first parameter should result string, , second parameter formatting. how copy byte character our current position in 1 register our current position in register? let's assume we've assigned our parameters 2 registers:

movl 8(%ebp),%edx # %edx = result-string movl 12(%ebp),%ecx # %ecx = format-string 

i tried following in loop:

movb (%ecx), %al movb %al, (%edx) # copy current character current position in result register incl %ecx incl %edx 

but result string contains a (the first character in string), , not full string expected.

all appreciated because comparison problem (question 1) keeping me stuck.

in regards question 1, appears comparing single byte chars 'cmpl' should 'cmpb' when checking escape character. need load character register. i'm not familiar at&t assembly, hope correct.

before loop:

movb .escape_string, %al 

comparison:

cmpb %al, %(ecx) 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -