我正在做这个小任务。 C代码传入值 基本上是函数,如果找不到该字符,将返回传入的字符串中第一个字符'c'的偏移量,返回-1
目前我在cld
和POP ecx
说must be the same size
mov edi, BYTE PTR [esi + eax]
说invalid instruction operands
在PROCS上遇到错误expression expected
我真的不知道该怎么办。
这是C
#include <stdint.h>
#include <stdio.h>
extern "C" {
int32_t findFirstChar(char *, char);
}
static char *testStrings[] = {
"",
"#",
"abc",
"abcd",
"abdefg",
"ABCdefg",
"bcdefgh",
"abdefabcd",
"abcdefgh",
"abcabcabcda",
};
#define NUM_TEST_CASES (sizeof (testStrings) / sizeof (*testStrings))
static char testChars[] = { 'c', 'd' };
int main()
{
printf("CSIS ICE 11\n\n");
//int f = findFirstChar(testStrings2[2], 'd');
for (int i = 0; i < sizeof(testChars); i++) {
char c = testChars[i];
for (int j = 0; j < NUM_TEST_CASES; j++) {
char *s = testStrings[j];
printf("Char [%c] String [%s] offset [%d]\n", c, s, findFirstChar(s, c));
}
printf("\n");
}
return 0;
}
这是asm(x86)Masm
.386P ; use 80386 instruction set
.MODEL flat,C ; use flat memory model
printf PROTO C, :VARARG
.DATA ; declare initialzed data here
target DWORD 100 DUP(?) ;storage space
.STACK ; use default 1k stack space
doPrologue MACRO nvars
PUSH ebp ; save caller base pointer
MOV ebp, esp ; set our base pointer
PUSH edi
PUSH esi ; end prologuee
ENDM
doEpilogue MACRO
POP esi ; start epilogue
POP edi
MOV esp, ebp ; deallocate locals
POP ebp ; restore caller base pointer
ENDM
.CODE ; contains our code
stringLength PROC PUBLIC
doPrologue
MOV esi, [ebp + 8]
MOV eax, 0
cld ; clear destination
mov esi, BYTE PTR [esi + eax] ;start
mov edi, OFFSET target ;emd
INC ecx
rep movsd ;will inc ecx by 1 a as everything is getting coppied
PUSH ecx ;saves the counter number
doEpilogue
RET
stringLength ENDP
;----------------------------------------------------------------------------
findFirstChar PROC PUBLIC
doPrologue
;CALL stringLength
POP ecx
mov edi, BYTE PTR [esi + eax]
mov al, [eax] ; search for 'eax'
;ecx is already counting length
cld
repne scasb ; repeat while not equal
jnz negOne ; no value found so, -1
MOV eax, edi ; get the number of which the char is at
doEpilogue;
RET
negONE:
MOV eax,-1
doEpilogue
RET
findFirstChar ENDP
END