简体   繁体   English

运行以SSE4.2指令汇编编写的strlen()时,程序崩溃

[英]The program crashes when running strlen() written in assembly with SSE4.2 instructions

My program is below and I want to use it to calculate the length of a string. 我的程序在下面,我想用它来计算字符串的长度。

.CODE
EQUAL_EACH = 1000b
strlen_sse PROC
string equ [esp+4]
mov ecx, string
;ecx = string
mov eax, -16
mov edx, ecx
pxor xmm0, xmm0

STRLEN_LOOP:
    add eax, 16
    PcmpIstrI xmm0, xmmword ptr [edx+eax], EQUAL_EACH
    jnz STRLEN_LOOP

add eax, ecx
ret

strlen_sse ENDP
END

It always crashes when I run it. 当我运行它时,它总是崩溃。 What caused this? 是什么原因造成的? 在此处输入图片说明

The main is like below: main内容如下:

#include<stdio.h>
#include<windows.h>
extern "C" int strlen_sse(const char* src);

int main(){
    DWORD start,stop;
    char* str = "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf";
    start = GetTickCount();
    for (; i < 100000000; i++) {
        strlen_sse(str);
    }
    stop = GetTickCount();
    printf("%lld ms\n",stop-start);
}

My working environment is visual studio 2015 community , the platform of the project is x64(active) , there is not any errors when I compile it. 我的工作环境是visual studio 2015 community ,项目的平台是x64(active) ,编译时没有任何错误。

How can I fix this bug? 如何解决此错误?

The C code is probably compiled to 64bit code, and the assembly is 32 bit code. C代码可能已编译为64位代码,而汇编则为32位代码。 Having 32 bit code in a 64 bit code segment will not work. 在64位代码段中包含32位代码将不起作用。

Change the assembly to this (I removed unnecessary code aswell): 将程序集更改为此(我也删除了不必要的代码):

.CODE
EQUAL_EACH = 1000b
strlen_sse PROC
    mov rax, -16
    mov rdx, [rsp + 8]
    pxor xmm0, xmm0

    STRLEN_LOOP:
        add rax, 16
        PcmpIstrI xmm0, xmmword ptr [rdx+rax], EQUAL_EACH
        jnz STRLEN_LOOP

    add rax, rcx
    ret

strlen_sse ENDP
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM