user1078516
user1078516

Reputation:

How to write Sparc assembly and run its binary in Qemu or Simics?

I am trying to start writing some Sparc assembly, but I can't figure out how to assemble and run the code. I have written arc with arcTools, but that's as far as I have gone with assembly. I have downloaded both simics and qemu, but I don't know where to go from here. Can anyone point me in the right direction? Thanks.

Upvotes: 4

Views: 1550

Answers (1)

Jester
Jester

Reputation: 58762

You didn't say what operating system(s) you use. For this example, I will assume you have linux and want to write simple standalone sparc code (for educational purposes). You will need binutils and gdb compiled for sparc and qemu-sparc. Save this small sample code as test.s:

.globl _start
_start:
    mov %o0, %g0
1:
    inc %o0
    cmp %o0, 100
    bl 1b
    nop
    b .
    nop

Use as to assemble and ld to link, as follows:

$ sparc-linux-as -g -o test.o test.s
$ sparc-linux-ld -g -o test test.o

Should produce the binary test:

$ file test
test: ELF 32-bit MSB executable, SPARC, version 1 (SYSV), statically linked, not stripped

Now start qemu-sparc set up for gdb remote debugging (pick a port of your choice, I used 1234):

$ qemu-sparc -g 1234 test

It will wait for gdb to connect. In another terminal, start gdb for the binary:

$ sparc-linux-gdb test
GNU gdb (GDB) 7.3.50.20111117-cvs-debian
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-unknown-linux-gnu --target=sparc-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /var/tmp/test...done.
(gdb)

Attach to the qemu instance:

(gdb) target remote :1234
Remote debugging using :1234
_start () at test.s:3
3           mov %o0, %g0

From here on, you can use gdb as usual to execute your code, examine registers and memory.

Upvotes: 6

Related Questions