Strange Characters when printing a string in x86 assembly language?

0

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

string
assembly
x86
asked on Stack Overflow Sep 25, 2020 by Akash

2 Answers

2

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.

answered on Stack Overflow Sep 25, 2020 by Nate Eldredge • edited Sep 25, 2020 by Nate Eldredge
-1

To get rid of the special characters write the strlen function before the start process and create a new register for the newline character

answered on Stack Overflow Sep 25, 2020 by M.Sajid

User contributions licensed under CC BY-SA 3.0