Renato
Renato

Reputation: 302

Help with assembly code

I'm trying to learn assembly for various reasons and I'm trying to write a code that says hello bla bla bla then reads your name, prints your name and says more bla bla bla but it does not show the text after it gets your name Im sure its something stupid... sorry...

here is what I have so far:

.section .bss
    .lcomm bufname, 256 
.section .data
    msg1:
    .ascii  "Hello, please enter your name!\n"  # Message to write
    len1 = . - msg1                 # Length of string

    msg2:
    .ascii "Good to meet you "
    len2 = . - msg2

    msg3:
    .ascii ". Iam your first assembly program!\n"
    len3 = . - msg3

.section .text

.globl _start

_start:
    call get_name
get_name:
    #should print msg1
    mov $4, %eax        # system call number (sys_write)
    mov $1, %ebx        # file descriptor (stdout)
    mov $msg1, %ecx     # Message to write
    mov $len1, %edx     # Lenght of message
    int $0x80       # Call kernel

    #should get user input
    mov $3, %eax        # System call number (sys_read)
    mov $0, %ebx        # File descriptor (stdin)
    mov $bufname, %ecx  # Buffer to store the name
    mov $256, %edx      # Lenght of buffer
    int $0x80 

    #should print msg2    
    mov $4, %eax
    mov $1, %ebx
    mov $msg2, %ecx
    mov $len2, %edx
    int $0x80

    #should print bufname (doesn't)
    mov $bufname, %ecx
    mov $256, %edx  
    int $0x80

    #should print msg3 (doesn't) 
    mov $msg3, %ecx
    mov $len3, %edx
    int $0x80 

    call exit   
exit:
    mov $1, %eax
    mov $0, %ebx
    int $0x80 

To compile i use

as Jes.s -o Jes.o
ld Jes.o -o Jes

This is the output i get

$ ./Jes
Hello, please enter your name!
renato
Good to meet you

It should show

Good to meet you renato. Iam your first assembly program!

Why is this wrong? and much thanks for your time!

Upvotes: 2

Views: 696

Answers (2)

MRAB
MRAB

Reputation: 20644

When you're printing the name, you're assuming that EAX and EBX are unchanged from the previous sys_write. That may not be the case.

Also, you're passing the length of the buffer when writing the name. Are you sure that you shouldn't be passing the length of just the name? The sys_read should've returned the length of what was read. (You may need to save it somewhere while you're printing msg2.)

Upvotes: 2

mpontillo
mpontillo

Reputation: 13947

The problem is that when you call the interrupt (int) the registers can get overwritten. It's not like making a function call where the registers are saved and restored for you. You need to place duplicates of these lines before all of your int calls:

mov $4, %eax        # system call number (sys_write)
mov $1, %ebx        # file descriptor (stdout)

Upvotes: 1

Related Questions