So I have this C file where I am getting two strings of input from stdin and I am trying to pass them to my assembly code and have my assembly code determine if the first string would come after the second string in a dictionary and if so then return a 1.
#include <stdio.h>
extern int stringcheck(char *s1, char *s2);
int main(){
char s1[30];
fgets(s1, 31, stdin);
char s2[31];
fgets(s2, 31, stdin);
int ret = stringcheck(s1, s2);
if(ret == 1){
printf("True\n");
}
if(ret == 0){
printf("False\n");
}
return 0;
}
In this assembly code I tried to load my strings into appropriate registers and then use cmpsb in a loop and have the program jump to 'second' if the carry flag is set to 1. What happens though is that regardless of the string entered the program will always jump to the 'second' block and return 9 so I'm assuming I messed something up with how I prepared the strings to be checked.
global stringcheck
section .text
stringcheck:
; save state of registers and setup stack frame
push rbp
mov rbp, rsp
push rax
push rbx
push rdx
lea rsi, [rbp+60] ; move first arg into rsi
lea rdi, [rbp+30] ; move second arg into rdi
mov rcx, 30 ; set up incrementer
cld
top: cmpsb
jc second
loop top
jmp first
first: pop rdx
pop rbx
pop rax
mov rsp, rbp
pop rbp
mov rax, 1
ret
second: pop rdx
pop rbx
pop rax
mov rsp, rbp
pop rbp
mov rax, 0
ret