X86 NASM大会将低级到鞋面和鞋帮​​为小写字符 [英] X86 NASM Assembly converting lower to upper and upper to lowercase characters

查看:196
本文介绍了X86 NASM大会将低级到鞋面和鞋帮​​为小写字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我是pretty新的组装,我在关于如何我应该从小写如果用户在装配一个大写字母,反之亦然转换为大写的几个问题。以下是我迄今为止:

As i am pretty new to assembly, i have a few questions in regards to how i should convert from a lowercase to an uppercase if the user enters an uppercase letter or vice versa in assembly. here is what i have so far:

section .data
Enter db "Enter: "
Enter_Len equ $-Enter

Output db "Output: "
Output_Len equ $-Output

Thanks db "Thanks!"
Thanks_Len equ $-Thanks

Loop_Iter dd 0 ; Loop counter

section .bss
In_Buffer resb 2
In_Buffer_Len equ $-In_Buffer

section .text
global _start

_start:
    ; Print Enter message
    mov eax, 4 ; sys_write
    mov ebx, 1
    mov ecx, Enter
    mov edx, Enter_Len
    int 80h

    ; Read input
    mov eax, 3 ; sys_read
    mov ebx, 0
    mov ecx, In_Buffer
    mov edx, In_Buffer_Len
    int 80h

所以基本上,如果我是正确的,我的EDX包含输入的字符串。现在,来自下到上和上转换为小写的窘境。由于我绝对新来这个,有字面上不知道该怎么做。任何帮助将是非常美联社preciated:)

So basically, if i am correct, my edx contains the string entered. Now comes the dilemma of converting from lower to upper and upper to lowercase. As i am absolutely new to this, have literally no clue what to do. Any help would be much appreciated :)

推荐答案

如果你只支持ASCII,那么你可以使用强制将小写字母或0x20的

If you only support ASCII, then you can force lowercase using an OR 0x20

  or   eax, 0x20

同样,您可以将信件通过清除该位为大写:

Similarly, you can transform a letter to uppercase by clearing that bit:

  and  eax, 0xBF   ; or use ~0x20

和作为nneonneo提到的,字符大小写可以使用 XOR 指令进行交换:

And as nneonneo mentioned, the character case can be swapped using the XOR instruction:

  xor  eax, 0x20

这只是工作,如果 EAX 是'A'到'z'或'A'和'Z'之间,所以你必须进行比较,并确保你的范围是:

That only works if eax is between 'a' and 'z' or 'A' and 'Z', so you'd have to compare and make sure you are in the range:

  cmp  eax, 'a'
  jl   .not-lower
  cmp  eax, 'z'
  jg   .not-lower
  or   eax, 0x20
.not-lower:

我用NASM语法。您可能希望确保 JL JG 是正确的太...

如果您需要转换的任何国际字符,那么这是一个复杂多了,除非你可以调用tolower的一个的libc()或TOUPPER()函数接受统一code字符。

If you need to transform any international character, then that's a lot more complicated unless you can call a libc tolower() or toupper() function that accept Unicode characters.

作为一个公平的问题:为什么要工作? (由kuhaku问)

As a fair question: why would it work? (asked by kuhaku)

ASCII字符(也通过了ISO-8859)拥有的0x41和和5AH即可与0x61之间0x7A小写字符之间定义的基本大写字符。

ASCII characters (also ISO-8859-1) have the basic uppercase characters defined between 0x41 and 0x5A and the lowercase characters between 0x61 and 0x7A.

要强制四成及6成5 7,你强迫第5位(0x20的)进行设置。

To force 4 into 6 and 5 into 7, you force bit 5 (0x20) to be set.

要到大写,你却反其道而行之,删除第5位,使其成为零。

To go to uppercase, you do the opposite, you remove bit 5 so it becomes zero.

这篇关于X86 NASM大会将低级到鞋面和鞋帮​​为小写字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆