繁体   English   中英

如何在 ARM 程序集中获取字符串中字符的 ASCII 值?

[英]How can I get the ASCII value of characters in a string in ARM Assembly?

我必须将以下代码转换为汇编语言:

void rotN1(char *str, int n)
{ // NB: str will be modified in-place
  char *p;
  for (p = str; *p != '\0'; p++)
  {
    int currChar = (int)*p;
    if (currChar >= 'a' && currChar <= 'z')
    {
      currChar = currChar + n;
      if (currChar > 'z')
      {
        currChar = currChar - 26;
      }
      *p = (char)currChar;
    }
  }
}

它基本上是在给定字符串 (str) 上使用凯撒密码,n 是加密密钥。



.data
        array:  .string         %[str]
        .equ    len.array,.-array
        .align

.text
        .global  main
main:

        nop

        ldr r2,=array           // pointer
        MOV r0, #0              // initialise loop index to 0
        MOV r1, #len.array      // number of elements 
    MOV r4, %[n]
        Loop:
                ldrb r3, [r2, r0]
            mov r6, # 
            B check_A 
            B check_Z
            ADD r3, r3, r4
            

        ADD r0, r0, #1           //increment loop index
        CMP r0, r1
        BLE Loop


_exit:
        mov r7, #1
        svc 0



check_A:
CMP r3, #97
BLT _exit

check_Z:
CMP r3, #122
BGT _exit

我还没有完成代码,但是在遍历数组时,我需要在每次迭代中获取该字符的 ASCII 值。 有没有办法做到这一点? 我必须在 C 中作为内联 ARM 执行此操作。

“...我需要在每次迭代中获取该字符的 ASCII 值。”

你已经有了它们。

每个ASCII字符都有一个值,如链接表中所示。 使用十进制值引用它们,字符AZ的范围为65 - 90 az范围为97 - 122 因此,在一个数组中,例如:

char msg[] = "Hello";

msg[0]的值为72
msg[4]的值为111
msg[5]的值为0

注意,最后一次转换是因为msg是用字符串文字初始化的,而C中的字符串文字被定义为以NULL终止符结尾的char数组。 请注意,并非所有char arrays 都需要NULL终止,在这种情况下,该数组将是一个简单的char数组,而不是C字符串。

此外,正如您在帖子中显示的那样,用单引号括起来的 ASCII 字符表示该字符的十进制值。 例如在表达式中

char aitch = 'H';     

aitch现在的值为 72。

暂无
暂无

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

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