x86转换为小写程序集

该程序将char指针转换为小写。 我正在使用Visual Studio 2010。

这是另一个问题,但更简单易读,更直接。

 int b_search (char* token) { __asm { mov eax, 0 ; zero out the result mov edi, [token] ; move the token to search for into EDI MOV ecx, 0 LOWERCASE_TOKEN: ;lowercase the token OR [edi], 20h INC ecx CMP [edi+ecx],0 JNZ LOWERCASE_TOKEN MOV ecx, 0 

在我的OR指令中,我试图将包含地址的寄存器更改为全部小写,我一直得到未处理的exception…访问冲突,没有括号什么,我没有得到错误但没有变得更低。 任何建议? 这是另一个问题的一些更大的代码的一部分,但我把它分解了,因为我只需要这个解决方案。

您的代码只能改变第一个字符(或[edi],20h) – EDI不会增加。

编辑:发现此线程与解决方法。 尝试使用’dl’而不是al。

 ; move the token address to search for into EDI ; (not the *token, as would be with mov edi, [token]) mov edi, token LOWERCASE_TOKEN: ;lowercase the token mov al, [edi] ; check for null-terminator here ! cmp al, 0 je GET_OUT or al, 20h mov dl, al mov [edi], dl inc edi jmp LOWERCASE_TOKEN GET_OUT: 

我将数据加载到寄存器中,在那里操作,然后将结果存储回内存。

 int make_lower(char* token) { __asm { mov edi, token jmp short start_loop top_loop: or al, 20h mov [edi], al inc edi start_loop: mov al, [edi] test al, al jnz top_loop } } 

但请注意,您转换为大写字母存在一些缺陷。 例如,如果输入包含任何控制字符,它会将它们更改为其他内容 – 但它们不是大写字母,并且它们将它们转换为不会是小写字母。

问题是,OR运算符像许多其他运算符一样不允许两个内存或常量参数。 这意味着:OR运算符只能有以下参数:

 OR register, memory OR register, register OR register, constant 

第二个问题是,OR必须将结果存储到寄存器,而不是存储器。 这就是为什么在设置括号时会出现访问冲突的原因。 如果删除括号,参数都可以,但是你没有把小写字母写入内存,你打算做什么。 所以使用另一个寄存器,将信件复制到,然后使用OR。 例如:

 mov eax, 0 ; zero out the result mov edi, [token] ; move the token to search for into EDI MOV ecx, 0 LOWERCASE_TOKEN: ;lowercase the token MOV ebx, [edi] ;## Copy the value to another register ## OR ebx, 20h ;## and compare now the register and the memory ## MOV [edi], ebx ;##Save back the result ## INC ecx CMP [edi+ecx],0 JNZ LOWERCASE_TOKEN MOV ecx, 0 

这应该工作^^