You are to write a program that will allow us to experiment with Square1 sequenc
ID: 3669239 • Letter: Y
Question
You are to write a program that will allow us to experiment with Square1 sequences.
I want you to add two procedures: (1) one called “terms” (called by the main program) whose job is to display up to the first 10 terms in a sequence that starts with the value that it is passed (in $a0) and (2) a function called “rev” that will be called by the procedure “terms” … this function will actually calculate and return (in $v0) the reverse of the current term that is passed to it (in $a0). Remember that since you have procedures calling other procedures, you need to be concerned with preserving and restoring certain values on the stack.
IMPORTANT NOTE: The procedure terms will be the lengthy one … the one that has a loop that produces and prints up to 10 values for the sequence. The rev function will be fairly short (probably less than 10 lines of actual code). Its sole job is to take its integer parameter and return an integer that has the digits reversed (e.g., if 123 is passed in $a0, the value 321 is returned in $v0).
# Your name/date
# Appropriate documentation
# insert your terms procedure and your rev function here
main: la $a0, intro # print intro
li $v0, 4
syscall
loop: la $a0, req # request value of n
li $v0, 4
syscall
li $v0, 5 # read value of n
syscall
ble $v0, $zero, out # if n is not positive, exit
move $a0, $v0 # set parameter for terms procedure
jal terms # call terms procedure
j loop # branch back for next value of n
out: la $a0, adios # display closing
li $v0, 4
syscall
li $v0, 10 # exit from the program
syscall
.data
intro: .asciiz "Welcome to the Square1 tester!"
req: .asciiz " Enter an integer (zero or negative to exit): "
adios: .asciiz "Come back soon! "
Explanation / Answer
The program below is defined by keeping in mind the concept of system programming.
Although I have not used the variable and all the stuff that is defined above, I have a program that will provide you the full functionality of what it is expected.
.Ltext0:
.section .rodata
.align 8
.LC0:
.string "Welcome to the Square1 tester!"
.align 8
.LC1:
.string " Enter an integer (zero or negative to exit): "
.LC2:
.string "%d"
.LC3:
.string "enter the value from 1 to 10"
.LC4:
.string "Come back soon!"
.text
.globl main
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $0, -8(%rbp)
movl $10, -4(%rbp)
movl $.LC0, %edi
movl $0, %eax
call printf
movl $.LC1, %edi
movl $0, %eax
call printf
leaq -8(%rbp), %rax
movq %rax, %rsi
movl $.LC2, %edi
movl $0, %eax
call __isoc99_scanf
movl -8(%rbp), %eax
testl %eax, %eax
jle .L2
movl $.LC3, %edi
movl $0, %eax
call printf
movl -4(%rbp), %eax
movl %eax, %edi
call terms
.L2:
movl $.LC4, %edi
call puts
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.globl terms
terms:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movl %edi, -20(%rbp)
.LBB2:
movl $0, -4(%rbp)
jmp .L5
L6:
addl $1, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %esi
movl $.LC2, %edi
movl $0, %eax
call printf
.L5:
cmpl $10, -4(%rbp)
jle .L6
.LBE2:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.section .rodata
.LC5:
.string "The reverse is %d"
.text
.globl rev
rev:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movl %edi, -20(%rbp)
movl $0, -12(%rbp)
movl $0, -8(%rbp)
movl $0, -8(%rbp)
jmp .L8
.L11:
.LBB3:
movl -8(%rbp), %eax
movl %eax, -4(%rbp)
jmp .L9
.L10:
movl -12(%rbp), %edx
movl %edx, %eax
sall $2, %eax
addl %edx, %eax
addl %eax, %eax
movl %eax, -12(%rbp)
movl -4(%rbp), %ecx
movl $1717986919, %edx
movl %ecx, %eax
imull %edx
sarl $2, %edx
movl %ecx, %eax
sarl $31, %eax
subl %eax, %edx
movl %edx, %eax
sall $2, %eax
addl %edx, %eax
addl %eax, %eax
subl %eax, %ecx
movl %ecx, %edx
addl %edx, -12(%rbp)
movl -4(%rbp), %ecx
movl $1717986919, %edx
movl %ecx, %eax
imull %edx
sarl $2, %edx
movl %ecx, %eax
sarl $31, %eax
subl %eax, %edx
movl %edx, %eax
movl %eax, -4(%rbp)
.L9:
cmpl $0, -4(%rbp)
jne .L10
movl -12(%rbp), %eax
movl %eax, %esi
movl $.LC5, %edi
movl $0, %eax
call printf
.LBE3:
addl $1, -8(%rbp)
.L8:
cmpl $10, -8(%rbp)
jle .L11
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.Letext0:
The calculation for the reverse function has been done for the number from 1 to 10 only. If needed we can modify in the loop itsef to check out for the changes.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.