如何访问char数组并将小写字母更改为大写,反之亦然(How to access a char array and change lower case letters to upper case, a

编程入门 行业动态 更新时间:2024-10-25 04:26:01
如何访问char数组并将小写字母更改为大写,反之亦然(How to access a char array and change lower case letters to upper case, and vice versa)

我目前正在使用x86处理器开发结构化计算机组织的类项目。 我访问的值是一个1字节的char,但我不知道如何将它与大写字母进行比较。 他们说要使用十六进制格式的ASCII表,但我不确定如何比较两者。

void changeCase (char char_array[], int array_size ) {
    __asm {
            // BEGIN YOUR CODE HERE

        mov eax, char_array;        //eax is base image
        mov edi, 0;

    readArray:
        cmp edi, array_size;
        jge  exit;
        mov ebx, edi;           //using ebx as offset
        shl ebx, 2;
        mov cl, [eax + ebx];    //using ecx to be the storage register

    check:
        //working on it
        cmp cl, 0x41;       //check if cl is <= than ASCII value 65 (A)
        jl next_indx;
        cmp cl, 0x7A;       //check if cl is >= than ASCII value 122 (z)
        jg next_indx;
        cmp cl, 'a';
        jl convert_down;
        jge convert_up;


    convert_down:
        or cl, 0x20;        //make it lowercase
        jmp write;

    convert_up:
        and cl, 0x20;       //make it uppercase
        jmp write;

    write:
        mov byte ptr [eax + ebx], cl    //slight funky town issue here,

    next_indx:
        inc edi;

    exit:
        cmp edi, array_size;
        jl readArray;

    mov char_array, eax;
            // END YOUR CODE HERE
    }
}
 

在这一点上任何事情都有帮助。 提前谢谢你的帮助!

编辑1:

感谢所有的建议和清晰点,编辑我的代码以反映变化。 现在存在访问冲突的一些问题。

编辑2(+):

感谢有用的眼睛的人。 我现在还在翻译所有的信件。

I'm currently working on a class project for Structured Computer Organization using an x86 processor. The value that I am accessing is an 1 byte char, but I do not know how to compare it to an uppercase. They said to use an ASCII table of the hex format, but I'm not sure how to even compare the two.

void changeCase (char char_array[], int array_size ) {
    __asm {
            // BEGIN YOUR CODE HERE

        mov eax, char_array;        //eax is base image
        mov edi, 0;

    readArray:
        cmp edi, array_size;
        jge  exit;
        mov ebx, edi;           //using ebx as offset
        shl ebx, 2;
        mov cl, [eax + ebx];    //using ecx to be the storage register

    check:
        //working on it
        cmp cl, 0x41;       //check if cl is <= than ASCII value 65 (A)
        jl next_indx;
        cmp cl, 0x7A;       //check if cl is >= than ASCII value 122 (z)
        jg next_indx;
        cmp cl, 'a';
        jl convert_down;
        jge convert_up;


    convert_down:
        or cl, 0x20;        //make it lowercase
        jmp write;

    convert_up:
        and cl, 0x20;       //make it uppercase
        jmp write;

    write:
        mov byte ptr [eax + ebx], cl    //slight funky town issue here,

    next_indx:
        inc edi;

    exit:
        cmp edi, array_size;
        jl readArray;

    mov char_array, eax;
            // END YOUR CODE HERE
    }
}
 

Anything helps at this point. Thank you in advance for the help!

edit 1:

Thanks for all the suggestion and points of clarity, edited my code to reflect change. Some problem with access violation now.

edit 2 (+):

Thanks for the helpful eyes people. I'm still getting to translating all letters now.

最满意答案

为了清楚起见,我将使用纯装配并假设......

char_array是[ebp+8]的32位指针。 array_size是[ebp+12]处的二进制补码32位数。 对于你的平台(无论如何这都是这种方式), char的编码是ASCII。

您应该能够将自己推断为内联汇编。 现在,如果你看看桌子,每个人都应该记住,但几乎没有人这样做 ,你会注意到一些重要的细节......

大写字母A到Z映射到代码0x41到0x5A 。 小写字母a到z映射到代码0x61到0x7A 。 其他一切都不是一封信,因此不需要转换大小写。 如果你看一下大写和小写字母范围的二进制表示,你会注意到它们完全相同,唯一的例外是大写字母的第6位被清除,而小写字母则被设置。

结果,算法将是......

while array_size != 0 byte = *char_array if byte >= 0x41 and byte <= 0x5A *char_array |= 0x20 // Turn it lowercase else if byte >= 0x61 and byte <= 0x7A *char_array &= 0xDF // Turn it uppercase array_size -= 1 char_array += 1

现在,我们将其转化为汇编......

mov eax, [ebp+8] # char *eax = char_array mov ecx, [ebp+12] # int ecx = array_size .loop: or ecx, ecx # Compare ecx against itself jz .end_loop # If ecx (array_size) is zero, we're done mov dl, [eax] # Otherwise, store the byte at *eax (*char_array) into `char dl` cmp dl, 'A' # Compare dl (*char_array) against 'A' (lower bound of uppercase letters) jb .continue # If dl` (*char_array) is lesser than `A`, continue the loop cmp dl, 'Z' # Compare dl (*char_array) against 'Z' (upper bound of uppercase letters) jbe .is_uppercase # If dl (*char_array) is lesser or equal to 'Z', then jump to .is_uppercase cmp dl, 'a' # Compare dl (*char_array) against 'a' (lower bound of lowercase letters) jb .continue # If dl (*char_array) is lesser than 'a', continue the loop cmp dl, 'z' # Compare dl (*char_array) against 'z' (upper bound of lowercase letters) jbe .is_lowercase # If dl (*char_array) is lesser or equal to 'z', then jump to .is_lowercase jmp .continue # All tests failed, so continue the loop .is_uppercase: or dl, 20h # Set the 6th bit mov [eax], dl # Send the byte back to where it came from jmp .continue # Continue the loop .is_lowercase: and dl, DFh # Clear the 6th bit mov [eax], dl # Send the byte back to where it came from jmp .continue # Continue the loop .continue: inc eax # Increment `eax` (`char_array`), much of like a pointer increment dec ecx # Decrement `ecx` (`array_size`), so as to match the previous pointer increment jmp .loop # Continue .end_loop:

一旦代码到达.end_loop ,你就完成了。

我希望这能为你带来光明!

For clarity's sake, I'll just use pure assembly and assume that...

char_array is a 32-bit pointer at [ebp+8]. array_size is a two's complement 32-bit number at [ebp+12]. For your platform (it is this way for most anyway), char's encoding is ASCII.

You should be able to deduce this yourself into inline assembly. Now, if you look at the table everyone is supposed to remember but barely anyone does, you'll notice some important details...

Uppercase letters A through Z map into codes 0x41 through 0x5A, respectively. Lowercase letters a through z map into codes 0x61 through 0x7A, respectively. Everything else is not a letter, and thus need no case conversion. If you look at the binary representation of the upper and lowercase letter ranges, you'll notice that they are exactly the same, with the sole exception that uppercase letters have bit 6 cleared, and lowercase ones have it set.

As a result, the algorithm would be...

while array_size != 0 byte = *char_array if byte >= 0x41 and byte <= 0x5A *char_array |= 0x20 // Turn it lowercase else if byte >= 0x61 and byte <= 0x7A *char_array &= 0xDF // Turn it uppercase array_size -= 1 char_array += 1

Now, let's translate this into assembly...

mov eax, [ebp+8] # char *eax = char_array mov ecx, [ebp+12] # int ecx = array_size .loop: or ecx, ecx # Compare ecx against itself jz .end_loop # If ecx (array_size) is zero, we're done mov dl, [eax] # Otherwise, store the byte at *eax (*char_array) into `char dl` cmp dl, 'A' # Compare dl (*char_array) against 'A' (lower bound of uppercase letters) jb .continue # If dl` (*char_array) is lesser than `A`, continue the loop cmp dl, 'Z' # Compare dl (*char_array) against 'Z' (upper bound of uppercase letters) jbe .is_uppercase # If dl (*char_array) is lesser or equal to 'Z', then jump to .is_uppercase cmp dl, 'a' # Compare dl (*char_array) against 'a' (lower bound of lowercase letters) jb .continue # If dl (*char_array) is lesser than 'a', continue the loop cmp dl, 'z' # Compare dl (*char_array) against 'z' (upper bound of lowercase letters) jbe .is_lowercase # If dl (*char_array) is lesser or equal to 'z', then jump to .is_lowercase jmp .continue # All tests failed, so continue the loop .is_uppercase: or dl, 20h # Set the 6th bit mov [eax], dl # Send the byte back to where it came from jmp .continue # Continue the loop .is_lowercase: and dl, DFh # Clear the 6th bit mov [eax], dl # Send the byte back to where it came from jmp .continue # Continue the loop .continue: inc eax # Increment `eax` (`char_array`), much of like a pointer increment dec ecx # Decrement `ecx` (`array_size`), so as to match the previous pointer increment jmp .loop # Continue .end_loop:

Once code reaches .end_loop, you're done.

I hope this has led a light on you!

更多推荐

本文发布于:2023-08-01 19:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1362847.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   数组   为大   字母   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!