Consider the following assembly language list file Write an
Solution
.globl main
.data
msgprompt: .word msgprompt_data
msgres1: .word msgres1_data
msgres2: .word msgres2_data
msgprompt_data: .asciiz \"Positive integer: \"
msgres1_data: .asciiz \"The worth of factorial(\"
msgres2_data: .asciiz \") is \"
# each call includes a stack section of twelve bytes, or 3 words.
# the house is reserved as follows:
# 0($sp) is reserved for the initial worth given to the current decision
# 4($sp) is that the house reserved for a come back worth
# 8($sp) is that the house reserved for the name and address.
# calls could manipulate their parent\'s information, however oldsters might not
# manipulate their child\'s information.
# i.e: if we\'ve got a decision A WHO includes a kid decision B:
# B could run:
# sw $t0, 16($sp)
# which might store information from $t0 into the parent\'s come back worth register
# A, however, ought to not(and, altogether cases I will consider, cannot) manipulate
# any information that belongs to a toddler decision.
.text
main:
# printing the prompt
#printf(\"Positive integer: \");
la $t0, msgprompt # load address of msgprompt into $t0
chemical element $a0, 0($t0) # load information from address in $t0 into $a0
li $v0, four # decision code for print_string
syscall # run the print_string syscall
# reading the input int
# scanf(\"%d\", &number);
li $v0, five # decision code for read_int
syscall # run the read_int syscall
move $t0, $v0 # store input in $t0
move $a0, $t0 # move input to argument register $a0
addi $sp, $sp, -12 # move stackpointer up three words
point $t0, 0($sp) # store input in prime of stack
point $ra, 8($sp) # store counter in spite of appearance of stack
jal factorial # decision factorial
# after we get here, we\'ve got the ultimate come back worth in 4($sp)
chemical element $s0, 4($sp) # load final come back val into $s0
# printf(\"The worth of \'factorial(%d)\' is: %d\ \",
la $t1, msgres1 # load msgres1 address into $t1
chemical element $a0, 0($t1) # load msgres1_data worth into $a0
li $v0, four # call for print_string
syscall # print worth of msgres1_data to screen
chemical element $a0, 0($sp) # load original worth into $a0
li $v0, one # call for print_int
syscall # print original worth to screen
la $t2, msgres2 #load msgres2 address into $t1
chemical element $a0, 0($t2) # load msgres_data worth into $a0
li $v0, four # call for print_string
syscall # print worth of msgres2_data to screen
move $a0, $s0 # move final come back worth from $s0 to $a0 for come back
li $v0, one # call for print_int
syscall # print final come back worth to screen
addi $sp, $sp, twelve # move stack pointer backpedal wherever we tend to started
# come back 0;
li $v0, ten # call for exit
syscall # exit!
.text
factorial:
# base case - still in parent\'s stack section
chemical element $t0, 0($sp) # load input from prime of stack into register $t0
#if (x == 0)
beq $t0, 0, returnOne # if $t0 is adequate zero, branch to returnOne
addi $t0, $t0, -1 # figure one from $t0 if not adequate zero
# algorithmic case - move to the current call\'s stack section
addi $sp, $sp, -12 # move stack pointer up three words
point $t0, 0($sp) # store current operating range into the highest of the stack section
point $ra, 8($sp) # store counter in spite of appearance of stack section
jal factorial # algorithmic decision
# if we tend to get here, then we\'ve got the kid come back worth in 4($sp)
chemical element $ra, 8($sp) # load this call\'s $ra again(we simply got in from a jump)
chemical element $t1, 4($sp) # load child\'s come back worth into $t1
chemical element $t2, 12($sp) # load parent\'s begin worth into $t2
# come back x * factorial(x-1); (not the come back statement, however the multiplication)
mul $t3, $t1, $t2 # multiply child\'s come back worth by parent\'s operating worth, store in $t3.
point $t3, 16($sp) # take result(in $t3), store in parent\'s come back worth.
addi $sp, $sp, twelve # move stackpointer backpedal for the parent decision
boy $ra # jump to parent decision
.text
#return 1;
returnOne:
li $t0, one # load one into register $t0
point $t0, 4($sp) # store one into the parent\'s come back worth register
boy $ra # jump to parent decision

