0

I've attached my code to this post. However, when I run it on gdb, once it scans the first number and second number, it gives me a "Program received signal SIGSEGV, Segmentation fault." error. I would appreciate any help to correct this. Thank you!

 .align 4
    .section        ".bss"
    input: .skip 4

    .section        ".data"
    format: .asciz "%d"
    string1: .asciz "Enter Number 1:\n"
    string2: .asciz "Enter Number 2:\n"
    string3: .asciz "The sum of %d and %d is %d\n"

    .section        ".text"

    .global main
    main:
    save %sp, -96, %sp

    set string1, %o0
    call printf
    nop
    set format, %o0
    set input, %o1
    call scanf
    nop
    set string2, %o0
    call printf
    nop
    set format, %o0
    set input, %o2
    call scanf
    nop
    add %o1, %o2, %o3
    set string3, %o0
    ld [%o1], %o1
    ld [%o2], %o2
    ld [%o3], %o3
    call printf
    nop
    ret
    restore

    mov 1, %g1
    ta 0
Jay
  • 73
  • 3
  • 14
  • 2
    Good that you tried to use `gdb` but try to use it to better effect ;) Look at which instruction is faulting and why. Also, comment your code, especially if you want others to help. The `add %o1, %o2, %o3` makes no sense whatsoever (adding two pointers). Furthermore you seem to rely on `%o` registers being preserved which are however callee-saved. Additonally, the second invocation of `scanf` sets `%o2` instead of `%o1`. – Jester Nov 27 '16 at 22:55
  • @Jester Thanks! I was able to figure out the answer thanks to your help. – Jay Nov 28 '16 at 05:27

2 Answers2

0

I think it should look more like this but I have never written SPARC assembly O:)

.align 4
.section        ".bss"
input1: .skip 4
input2: .skip 4

.section        ".data"
format: .asciz "%d"
string1: .asciz "Enter Number 1:\n"
string2: .asciz "Enter Number 2:\n"
string3: .asciz "The sum of %d and %d is %d\n"

.section        ".text"

.global main
main:
save %sp, -96, %sp

set string1, %o0
call printf

set format, %o0
set input1, %o1
call scanf

set string2, %o0
call printf

set format, %o0
set input2, %o1
call scanf

set input1, %o1
ld [%o1], %o1
set input2, %o2
ld [%o2], %o2
add %o1, %o2, %o3

set string3, %o0
call printf
nop
ret
restore

mov 1, %g1
ta 0
Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
0

I was able to figure out the problem thanks to the help Stefan and Jester!

! SungJae Kim

! b321024 ! Assignment 5 ! December 2nd, 2016

.align 4
.section    ".bss"
input1: .skip 4
input2: .skip 4

.section    ".data"
format: .asciz "%d"
string1: .asciz "Enter Number 1:\n"
string2: .asciz "Enter Number 2:\n"
string3: .asciz "The sum of %d and %d is %d\n"

.section    ".text"

.global main
main:
save %sp, -96, %sp

set string1, %o0
call printf
nop
set format, %o0
set input1, %o1
call scanf
nop
set string2, %o0
call printf
nop
set format, %o0
set input2, %o1
call scanf
nop
set input1, %o1
ld [%o1], %o1
set input2, %o2
ld [%o2], %o2
add %o1, %o2, %o3
set string3, %o0
call printf
nop
ret
restore

mov 1, %g1
ta 0
Jay
  • 73
  • 3
  • 14