for a small assignment we have to compare strings we were given this code in C
extern "C" {
bool isStringEqual(const char *s1, const char *s2);
}
static char *testStrings[] = {
"",
"Test String 1",
"Test String 2",
};
#define NUM_TEST_CASES (sizeof (testStrings) / sizeof (char *))
int main()
{
printf("CSIS\n\n");
for (int i = 0; i < NUM_TEST_CASES; i++) {
printf("Input string [%s] [%s] yields: %s\n ", testStrings[i], testStrings[i],
isStringEqual(testStrings[i], testStrings[i]) ? "true" : "false");
}
printf("Input string [%s] [%s] yields: %s\n ", testStrings[1], testStrings[2],
isStringEqual(testStrings[1], testStrings[2]) ? "true" : "false");
system("PAUSE");
return 0;
}
and we have to write the asm that does this logic
; while (*s1 != 0 && *s2 != 0) {
; if (*s1 != *s2)
; return false;
; }
; return true
Previously what we did before was we compared numbers which were input in C and then did some comparisons and calculations in asm. That was fine and I understood those completely, what I am having trouble here is understanding the C given and what are the strings (they seem to both look like "i" to me or possibly 1 and 2??) . And the second thing is finding out how I can compare the two strings. I understand how jumps and comparisons work and can make loops just fine just dont know how to go about comparing the strings...
Here is my asm that runs but will make everything still print out as
input
Input string [Test String 1] [Test String 1] yields: true
Input string [Test String 2] [Test String 2] yields: true
Input string [Test String 1] [Test String 2] yields: true
Press any key to continue . . .
My asm:
isStringEqual PROC PUBLIC
PUSH ebp ; save caller base pointer
MOV ebp, esp ; set our base pointer
PUSH edi
PUSH esi
;--------------------------------------------------------
; while (*s1 != 0 && *s2 != 0) {
; if (*s1 != *s2)
; return false;
; }
; return true
;-----------------------------------------------------------
mov esi, [ebp+8] ;get s1
mov edi, [ebp+12] ;get s2
MOV ebx, 0 ;move 0 into ebx
str_cmp_loop:
MOV al, [esi + ebx] ;goes right to left for string
CMP al, [edi + ebx] ;compares
JNE str_ret_false
CMP al, 0
JE str_ret_true
INC ebx
JMP str_cmp_loop
str_ret_false:
PUSH eax
PUSH OFFSET fmt ; push the string format
ADD esp, (2 * 4) ; clear the stack
POP esi
POP edi
MOV esp, ebp ; deallocate locals
POP ebp ; restore caller base pointer
RET
str_ret_true:
PUSH eax
PUSH OFFSET fmt ; push the string format
ADD esp, (2 * 4) ; clear the stack
POP esi
POP edi
MOV esp, ebp ; deallocate locals
POP ebp ; restore caller base pointer
RET
isStringEqual ENDP ; end the procedure
END isStringEqual
Can someone please just help me so I can go to sleep...