Modifying a character array, the modified part shows up backwards

 ̄綄美尐妖づ 提交于 2019-12-11 05:46:15

问题


I have just started learning assembly, and I am trying to modify a character array.

This is my assembly code:

.data
data byte 'Five', 0
.code 
    Asm proc
        lea rax, data
        mov dword ptr[rax], 'Four'
        ret
    Asm endp
end 

And my C++ code:

#include <stdio.h>
#include <conio.h>
// external function
extern "C" char* Asm();
// main function
int main()
{
    printf(Asm());
    _getch();
}

When I comment out mov dword ptr[rax], 'Four', the result is that the console prints: "Five". But, with the above code uncommented, the result is "ruoF", instead of what I expected it to be, which is obviously "Four".

Why is this happening? How can I get the text to output in the correct direction without having to do some cheap workaround like this: mov dword ptr[rax], 'ruoF'?


回答1:


You can use:

mov   ebx,'Four'
bswap ebx
mov   [rax],ebx

Or, on Intel Atom processors that support movbe (can be confirmed with cpuid):

mov   ebx,'Four'
movbe [rax],ebx


来源:https://stackoverflow.com/questions/14593122/modifying-a-character-array-the-modified-part-shows-up-backwards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!