【问题标题】:Can't find error!! Trying to loop throught a string and change lowercase to uppercase letter in x86 assembly language找不到错误!!尝试遍历字符串并将 x86 汇编语言中的小写字母更改为大写字母
【发布时间】:2018-08-13 13:11:37
【问题描述】:
section .data   
  msg db  "sum of x and y is " ;String
section .text
global _start
_start:
Change_letter:
  mov ECX, -1 ;set counter 
  mov ESI, [msg] ; move string address to ESI
  mov Eax , 32 ; mov 32 to eax for change lowercase to uppercase
startloop:
  inc ecx  ; 
  cmp byte [ESI+ecx], 0x00 ;compare with null 
  jne end
  cmp byte [ESI+ECX], 0x61 ; compare with lower bound of lowercase 
  jl startloop
  cmp byte [ESI+ECX], 0x7A
  jg startloop
  add byte [ESI+ECX], eax
end:
  ret

【问题讨论】:

  • 学习使用调试器。提示:你认为jne end 会做什么?
  • 你们必须承认这个程序运行得非常快。

标签: assembly x86 nasm


【解决方案1】:

你想要多少错误?

section .data   
  msg db  "sum of x and y is " ;String
section .text
global _start
_start:
Change_letter:
  mov ECX, -1 ;set counter 
  mov ESI, [msg] ; move string address to ESI

这会加载 esi 字符串的前 4 个字符,而不是地址。

  mov Eax , 32 ; mov 32 to eax for change lowercase to uppercase
startloop:
  inc ecx  ; 
  cmp byte [ESI+ecx], 0x00 ;compare with null 
  jne end

第一个字母不是零值,所以jne 将跳转到end:。此外,您没有在msg 中定义零字节,因此一旦将条件翻转为je,您就有可能在定义的msg 之后处理更多字节,直到意外发现一些随机零在内存中(实际上在msg 之后会有一个作为填充,所以除非你正确地推理你的代码,否则你不会注意到这个错误)。

  cmp byte [ESI+ECX], 0x61 ; compare with lower bound of lowercase 
  jl startloop

在处理 ASCII 值时,我倾向于以无符号的方式考虑它们,即jb,而不是jl。此外,您可以使用 NASM 'a' 而不是 0x61,它在 IMO 上更具可读性。

  cmp byte [ESI+ECX], 0x7A
  jg startloop

我还是宁愿使用无符号跳转ja'z' 常量。

  add byte [ESI+ECX], eax

这甚至如何编译...eax 是 32 位,而不是 8 位,因此 byte 关键字可能被忽略。如果您将所有警告都打开,NASM 可能会发出一些警告(我懒得尝试)。此外,您将32 添加到小写字母,因此从0x61'a',您将转到值0x81,在Linux 中当解释为7b ASCII 时是不可打印的字符(尽管使用UTF-8 编码或一些其他你可能会得到一些输出)。

end:
  ret

在损坏单个小写字母后,您会掉到end:

够了吗?并且使用调试器,通过阅读源代码来发现汇编错误需要多年的经验,即使只是阅读调试器屏幕通常也需要高度关注,才能真正注意到与您最初期望的细微差异,例如 0x91 而不是 0x61 几乎可以肯定第一眼看起来不错,等等...不要让你的大脑欺骗你,克服这些需要练习和技巧。

【讨论】:

  • add byte [ESI+ECX], eax 不与 NASM 2.13.02 一起组装。 error: mismatch in operand sizes.
猜你喜欢
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
  • 2013-02-04
  • 2019-06-08
  • 1970-01-01
相关资源
最近更新 更多