Question

I am new to assembly coding and I am trying figure out how the scanf function works. I just want to take the integer of the user input and add 5 to it and print that out. I can't get it to work and this is what I have so far. I tried just printing the user input by itself and I still can't get it to work.

Can someone explain to me the process?

.section ".data"
prompt: .asciz "\nEnter a number: "
format: .asciz "%d"
format2: .asciz "Your number plus 5 is: %d"
input: .word 0
nl: .asciz "\n"


define(a1, 5)

.align 4
.section ".text"
.global main
main:
save %sp, -96, %sp



!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!  Prompt the user for a number
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

set   prompt, %o0            !point o0 to the prompt
call  printf                 !call printf to print the prompt
nop

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!  uses scanf to store the integer in register o1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

set   format, %o0            !address of the format
set   input, %o1             !address of the location for the max
set   nl, %o2
call  scanf                  !reads user input, coverts to a
nop                          !number and stores at the memory referenced by input


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!Prints out the integer plus 5
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

set   format2, %o0
set   input, %o1
ld    [%o1], %o1             
add   %o1, %a1, %o1           
call   printf
nop


ret
restore
Was it helpful?

Solution

define(a1, 5) most probably does a simple text substitution, as such add %o1, %a1, %o1 will be assembled as add %o1, %5, %o1 whereby %5 will resolve to register number 5 which is %g5 and not what you want. Solution: just use add %o1, 5, %o1 or give some other symbolic name to the constant and use that without a % sign. The rest looks about right, except for the extra, but harmless, argument you pass to scanf (set nl, %o2).

Hint: saying "urgent" won't get you any faster replies, but providing more details, such as what the exact problem is, could.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top