I'm attempting to revisit assembly after several years of not having used it at all, and I'm having a bit of trouble with simple I/O. My program is designed to read a number from the user and spit it right back out. The "%c" is simply to remove the newline character (enter) from the buffer. This seems simple enough, and I can get it to work with strings, but for some reason I get a "Bus Error" after I input a number.

My code is as follows:

.section ".data"
informat: .asciz "%d%c"
outformat: .asciz "The number was: %d\n"
input: .word
returnkey: .byte

.section ".text"
.global main
.align 4
main:

save %sp, -96, %sp

set informat, %o0
set input, %o1
set returnkey, %o2
call scanf
nop

set outformat, %o0
set input, %o1
call printf
nop

ret
restore

In case my description of the problem doesn't make sense, my terminal window looks like the following:

#> gcc test.s
#> ./a.out
5
Bus Error
#>

This is on a Solaris 10 box. Any idea what I'm doing wrong?

Thanks in advance!

1. Write it in C
2. Compile with "gcc -S prog.c"
3. Inspect prog.s to find out how the compiler does it.
4. Figure out how you should have passed the ADDRESS of input and returnkey to the function.
5. Does .word really reserve the same space as an int in C ?
6. Do you need a .align 4 in front of your input declaration ?

Thanks for your reply, Salem. I think I have scanf working correctly now, but printf looks like it is printing an address. It is my understanding that printf doesn't need to be passed the address.

.section ".text"
.global main
main:

save %sp, -96, %sp

set instr, %o0
set [data], %o1
set [enterkey], %o2
call scanf
nop

set outstr, %o0
set data, %o1
call printf
nop

ret
restore

.section ".data"
instr: .asciz "%d"
outstr: .asciz "The number was: %d"
.align 4
data: .word
enterkey: .byte

This prints something like:

The number was: 133332

Forgive me if this seems like an elementary problem - its been 4 years since I've looked at any assembly, and I didn't know it well then. The book I'm looking at has poor examples, so I'm trying to piece together what I can from the web.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.