0

I am VERY new to assembly and I know that this problem should be an easy fix. I have hit a dilemma though. I am trying to create a function that will simply print a message that is sent to it on screen. Essentially do,

mov eax, 4 ; sys_write
mov ebx, 1 ; stdout 
mov ecx, message 
mov edx, message length
int 0x80 ; execute

but some reason the length parameter is not sending right or I am not referencing it right. This should be a simple solution and any help would be great! :D

global _start
section .data
    msg db "Hello, World!"
    msgS equ $ - msg



section .text
_start:


push msg ;The first param. MESSAGE
push msgS ;The second param. MESSAGE SIZE
call print

mov eax, 1 ; sys_exit
mov ebx, 0 ; exit code is 0
int 0x80 ; execute

print:
push ebp ; Push ebp's old location onto the stack.
mov ebp, esp ; set ebp to be esp's current location.

mov eax, 4
mov ebx, 1
mov ecx, [esp+8] ;The first param. MESSAGE (we jump by 8 instead of 4 because we just pushed ebp into the stack.)
mov edx, [esp+4] ;The second param. MESSAGE SIZE
int 0x80 


mov esp, ebp ; set esp back to what it was before this function
pop ebp ; set ebp back to what it was before this function
ret ; return back to before this function


When I run this code I do not get any results. I know that it has to be something wrong with the

mov edx, [esp+4]

code snippet because the string is 13 characters long. If I delete [esp+4] and replace it with 13 and than delete the push msgS before I called the function it works and prints the string.

I am very new to assembly and if you guys have any solution for me that would be awesome. I tried to google this, but I can not seem to find any results that work for me. I tried to rewrite and swap orders etc, but still did not work.

If I am doing a bad practice or something inside my code, correction is welcome.

P.S (Not concerning the question whatsoever). If you guys have any references where I can further study assembly (for newbie begineers) that would be great.

cdecde57
  • 21
  • 5
  • Normally we count stack args in order of ascending address, so the lowest address one is the first. But that's not your problem, it's that you moved ESP with `push ebp` before using `[esp+4]`. Run your program under `strace`, like `strace ./a.out` to see the args you actually pass to the syscall. Also use a debugger to see the value you get in registers as you single step, e.g. check where ESP is pointing vs. what's in memory there. Should help you understand what's wrong with what you did. – Peter Cordes Jun 20 '20 at 20:56
  • Yep, recheck your stack offsets. It may help to make a diagram of the stack layout, showing which register points where. – Nate Eldredge Jun 20 '20 at 20:59
  • Thank you guys! I will totally do that. Thanks for the answers :D – cdecde57 Jun 20 '20 at 21:01
  • 1
    Given your comment `we jump by 8 instead of 4 because we just pushed ebp into the stack`, you remembered ebp, but you're still off by 4, so I conclude you forgot about the return address. – Joseph Sible-Reinstate Monica Jun 20 '20 at 21:04

0 Answers0