2

我正在尝试将一个字符串存储在内存中,访问该字符串并将小写字母更改为大写,反之亦然。我不明白的是如何引用该地址的字符串地址和 ascii 值。我相信我可以弄清楚逻辑,只是在数据和地址操作方面存在问题。我如何区分两者?注意:这是一个家庭作业编码作业。

4

2 回答 2

0

暗示。用 a 翻转“32”位xor value,0x20以切换大小写。

在 Z80 汇编器上,它会像

SRC:    DB      'Press A Key To Continue', 0; Our source string
DST:    DB      'Press A Key To Continue', 0; Where we will put our string. make sure its big enough so we don't over write our program.
;load MSG+0 to working regist
;flip the 32 bit
;move the working register to DST + 0
;load MSG+1 and repeat
于 2011-01-23T08:12:19.077 回答
0

访问字符串会有所不同,具体取决于它是在闪存中还是在 RAM 中(我认为字符串可能会在启动时被复制到 RAM 中,但我并不肯定)。不过,鉴于这是一个家庭作业问题,您可能可以直接访问 RAM 中的字符串。正如@EnabrenTane 指出的那样,您只需翻转 char 字节的第 5 位即可更改大小写(阅读优秀的Wikipedia ASCII 页面以了解有关 ASCII 字节码的更多信息)。因此,如果您需要在 C 中执行此操作:

char the_string[6] = "foobar" // assuming string is created like this
int l = 6 * sizeof(char); // the length of the string

for(int i=0; i<l; i++) {
    char* c = the_string + i; // grab char[i] of the string
    *char = *char ^ 0x20; // flip the case
}

(我没有尝试编译这个,所以可能会有错误)

对于像上面那样创建的字符串,存储的值the_string实际上是指向第一个字符地址的指针。要在该字符串中查找特定字符,您只需添加到地址即可。要操作字符串,您需要取消引用您的char*指针 - 在本例中 - 将值存储回同一位置,覆盖原始值。

于 2011-01-24T18:04:54.537 回答