flat assembler
Message board for the users of flat assembler.

Index > Main > Not waiting for user input and somehow printing next string

Author
Thread Post new topic Reply to topic
ShreyasJejurkar



Joined: 22 Oct 2023
Posts: 6
ShreyasJejurkar 22 Oct 2023, 07:05
Hi there, I am new to assembly and so as FASM.

I am trying to write a program to add 2 numbers which I take from the user.
I am using Fasm under Ubuntu under WSL2 on W11.
Following is the program that I have written so far.

ignore utils.asm file here, it just contains a write macro which I created.

Code:
format ELF64 executable
entry main

segment readable executable

main:
    include 'utils.asm'
    
    write msg_first_number, msg_first_number.len
    
    mov rax, 0
    mov rdi, 1
    mov rsi, [first_number]
    mov rdx, [first_number.len]
    syscall
    
    exit 0

segment readable writeable

msg_first_number db "Enter your first number :  ", 0
msg_first_number.len db $ - msg_first_number

first_number dq 0
first_number.len dq $ - first_number

show db "Your number is ", 0
show.len db $ - show
    


When I execute the above program, I see it as output.


>./add
Enter your first number: Your number is <blink_cursor_here>


I do not understand why it's printing "Your number is " directly, even though I have never printed it in assembly code.
Post 22 Oct 2023, 07:05
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20121
Location: In your JS exploiting you and your system
revolution 22 Oct 2023, 14:15
You have to show all the code. We don't know what you have done there.
Post 22 Oct 2023, 14:15
View user's profile Send private message Visit poster's website Reply with quote
ShreyasJejurkar



Joined: 22 Oct 2023
Posts: 6
ShreyasJejurkar 22 Oct 2023, 14:16
This is what all my code is. I don't have anything else.
Post 22 Oct 2023, 14:16
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2439
Furs 22 Oct 2023, 14:24
Your memory layout for the 3 variables is one after the other. So it displays it from there when it tries to display first message, likely because your macro is wrong and has some sort of buffer overflow. Can you show the macro?

As for why the number itself, which is in-between, is not displayed, that's because it's initialized to NUL chars which aren't displayed.
Post 22 Oct 2023, 14:24
View user's profile Send private message Reply with quote
ShreyasJejurkar



Joined: 22 Oct 2023
Posts: 6
ShreyasJejurkar 22 Oct 2023, 14:54
Here is my other utils.asm file

Code:

macro exit exit_code
{
    mov rax, 60
    mov rdi, exit_code
    syscall
}

macro write msg, msg_len
{
    mov rax, 1
    mov rdi, 1
    mov rsi, msg
    mov rdx, msg_len
    syscall
}

; segment readable writeable
    
Post 22 Oct 2023, 14:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20121
Location: In your JS exploiting you and your system
revolution 22 Oct 2023, 15:46
The length is stored in a byte. But your macro uses the address, not the value.

You can either change len to a variable.
Code:
msg_first_number.len = $ - msg_first_number    
Or change the macro to read the length
Code:
    movzx rdx, [msg_len]    
Post 22 Oct 2023, 15:46
View user's profile Send private message Visit poster's website Reply with quote
ShreyasJejurkar



Joined: 22 Oct 2023
Posts: 6
ShreyasJejurkar 22 Oct 2023, 17:02
oh what a small mistake, After changing
Code:
msg_first_number.len db $ - msg_first_number
    

To =>
Code:
msg_first_number.len = $ - msg_first_number
    


it worked. I changed for other variables as well where we are calculating the length of it.

Also, I had to remove the square bracket of my
Code:
first_number    
variable when moving it to
Code:
rsi    
for some reason. Not sure why!?
Post 22 Oct 2023, 17:02
View user's profile Send private message Reply with quote
ShreyasJejurkar



Joined: 22 Oct 2023
Posts: 6
ShreyasJejurkar 22 Oct 2023, 17:04
And also how do I find this small issue in my code? @revolution

I mean what's the strategy or way you use to find the issue? Maybe that might help to find small issues like this when I encounter them in the future.

That will be a great help for me.
Post 22 Oct 2023, 17:04
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20121
Location: In your JS exploiting you and your system
revolution 22 Oct 2023, 17:10
You can use a debugger to follow the execution of each instruction one-by-one.

fdbg was created by a member of this board.
Post 22 Oct 2023, 17:10
View user's profile Send private message Visit poster's website Reply with quote
ShreyasJejurkar



Joined: 22 Oct 2023
Posts: 6
ShreyasJejurkar 22 Oct 2023, 17:15
how to use it, is it pre included in fasm? I installed FASM via APT on my ubuntu.
Post 22 Oct 2023, 17:15
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.