简体   繁体   中英

Strange Characters when printing a string in x86 assembly language?

My code is required to have a string that will be printed to the console, alongside a string length counting program that will count it instead of manually putting length of string in edx register. But i am getting strange characters printed right after the string is printed.


global          _start

section         .text

_start:

  mov           edi, message
  call          _strlen
  mov           edx, eax

  mov           eax, 4
  mov           ebx, 1
  mov           ecx, message
  int 80h

  mov           eax, 1
  mov           ebx, 5
  int 80h

 section         .data
message: db     "My name is Stanley Hudson", 0Ah
_strlen:

  push          ebx
  push          ecx

  mov           ebx, edi
  xor           al, al
  mov           ecx, 0xffffffff

  repne         scasb               ; REPeat while Not Equal [edi] != al

  sub           edi, ebx            ; length = offset of (edi – ebx)
  mov           eax, edi

  pop           ebx
  pop           ecx
  ret

Here is the output

strlen searches for a 0 byte terminating the string, but your string doesn't have one, so it goes until it does find a zero byte and returns a value that's too large.

You want to write

message: db     "My name is Stanley Hudson", 0Ah, 0
                                               ; ^^^

Another bug is that your _strlen function is apparently in the .data section, because you didn't go back to section .text after your string. x86-32 doesn't have the NX bit so the .data section is executable and everything still works, but it's surely not what you intend.

要摆脱特殊字符,请在启动过程之前编写 strlen 函数并为换行符创建一个新寄存器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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