Student Loan Program. Prompt for 1) an initial loan amount, 2) the annual percen
ID: 3634409 • Letter: S
Question
Student Loan Program.Prompt for
1) an initial loan amount,
2) the annual percentage rate, and
3) the desired monthly payments. Print out a repayment schedule with at least 4 columns: the month, month’s beginning balance, current months interest, and total paid.
Add text column headings at the top of the chart. Output the correct last payment amount and the total interest paid. (The special character ‘ ’, tab, can be used to align output columns)
current_months_interest = (beginning_balance * annual_percentage_rate) / ( 12 * 100)
beginning_balance = beginning_balance - ( payment – current_months_interest)
This problem should use integer arithmetic, not floating point. There are three levels of this program.
1) Basic (max score=90):
a) Integer arithmetic and whole dollar amounts.
b) Results displayed in whole dollars
2) Intermediate (max score=95):
a) Integer arithmetic and dollar amounts in cents.
b) Displayed in whole dollars
3) Advanced (max score=100):
a) Same as intermediate, but
b) results displayed in dollars and cents format
_____________________________________________________________________________________________________________
.data
getprin: .asciiz " Enter initial loan principal: "
getapr: .asciiz " Enter annual percentage rate (APR): "
getpay: .asciiz " Enter desired monthly payment: "
head1: .asciiz " Month Balance Int. Pd. Total Pd."
head2: .asciiz " ----- ------- -------- ---------"
complete: .asciiz " Completed - last payment is $"
tab: .asciiz " "
newline: .asciiz " "
nogo: .asciiz " Monthly payment too low to amortize loan; please reenter."
################################################################
# TEXT SEGMENT #
################################################################
.text
.globl main
main:
getdata:
# GET PRINCIPAL ---------------------------------------------------
li $v0, 4 # print_str syscall
la $a0, getprin # address for string
syscall
li $v0, 5 # read_int syscall
syscall
move $t0, $v0 # $s0 = PRINCIPAL
# GET APR ---------------------------------------------------------
li $v0, 4 # print_str syscall
la $a0, getapr # address for string
syscall
li $v0, 5 # read_int syscall
syscall
move $t1, $v0 # $s1 = APR
# GET MONTHLY PAYMENT----------------------------------------------
li $v0, 4 # print_str syscall
la $a0, getpay # address for string
syscall
li $v0, 5 # read_int syscall
syscall
move $t2, $v0 # $s2 = PAYMENT
# ERROR CHECK - MAKE SURE VALUES WILL AMORTIZE -------------------
li $t3, 1200 # load 1200 in $t0
divu $t4, $t1, $t3 # $t4 = APR / 1200
move $t1, $t4
mult $t1, $t0 # (APR/1200) * Principal
mflo $t7 # $t1 = (line above)
bge $t2, $t7, calc # branch to calc if monthly
# payment is big enough to amortize
li $v0, 4 # print_str syscall
la $a0, nogo # address for string
syscall
j getdata # jump back to getdata
calc:
# PRINT HEADER ---------------------------------------------------------
li $v0, 4
la $a0, head1
syscall
li $v0, 4
la $a0, head2
syscall
li $t3, 0
li $t4, 0
li $t5, 0
li $t6, 0
# $t0 = principal or balance
# $t1 = APR/1200 = MPR
# $t2 = monthly payment
# $t3 = payment # or month
# $t4 = current interest paid
# $t5 = total interest paid
# $t6 = total amount paid
loopstart:
# CALCULATIONS ------------------------------------------------------------
addi $t3, $t3, 1 # increment payment # or month
mult $t1, $t0 # current int pd = mpr * principal
mflo $t4
add $t5, $t5, $t4 # total int += current int
add $t6, $t6, $t2 # total paid += monthly payment
# PRINT NEWLINE -----------------------------------------------------------
li $v0, 4 # print_str syscall
la $a0, newline # address for string
syscall
# PRINT MONTH or PAYMENT NUM ----------------------------------------------
li $v0, 1 # system call code for print_int
move $a0, $t3 # integer to print
syscall # print it
move $t3, $a0 # move counter back to $t0
# PRINT TAB ---------------------------------------------------------------
li $v0, 4 # print_str syscall
la $a0, tab # address for string
syscall
# PRINT BALANCE -----------------------------------------------------------
li $v0, 1 # system call code for print_int
move $a0, $t0 # integer to print
syscall # print it
move $t0, $a0 # move balance back to $s0
# PRINT TAB (x2) ----------------------------------------------------------
li $v0, 4 # print_str syscall
la $a0, tab # address for string
syscall
li $v0, 4 # print_str syscall
la $a0, tab # address for string
syscall
# PRINT INTEREST PAID -----------------------------------------------------
li $v0, 1 # system call code for print_int
move $a0, $t5 # integer to print
syscall # print it
move $t5, $a0 # move tot. int. back to $t2
# PRINT TAB (x2) ----------------------------------------------------------
li $v0, 4 # print_str syscall
la $a0, tab # address for string
syscall
li $v0, 4 # print_str syscall
la $a0, tab # address for string
syscall
# PRINT TOTAL AMOUNT PAID -------------------------------------------------
li $v0, 1 # system call code for print_int
move $a0, $t6 # integer to print
syscall # print it
move $t6, $a0 # move balance back to $s0
# -------------------------------------------------------------------------
sub $t0, $t0, $t2 # calculate new balance
add $t0, $t0, $t4 # balance - payment + current int.
# END LOOP ----------------------------------------------------------------
bgt $t0, $t2, loopstart # if balance > monthly payment repeat loop
addi $t3, $t3, 1 # increment payment # or month
mult $t1, $t0 # current int pd = mpr * principal
mflo $t4
add $t0, $t0, $t4 # calculates last payment
# = balance + current interest
# PRINT NEWLINE -----------------------------------------------------------
li $v0, 4 # print_str syscall
la $a0, newline # address for string
syscall
# PRINT FINAL PAYMENT -----------------------------------------------------
li $v0, 4
la $a0, complete
syscall
li $v0, 1 # system call code for print_int
move $a0, $t0 # integer to print
syscall # print it
# END -------------------------------------------------------------------
li $v0, 10 # return to kernel from main routine
syscall
Correct or not?
Explanation / Answer
file "abc.c" .section .rodata .align 4 .LC0: .string "Please enter 1 to 5 (please hit enter after entering each number) " .LC1: .string "%d" .align 4 .LC2: .string "Please enter 40 to 45 (please hit enter after entering each number) " .LC3: .string "C[%d] = %d , " .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp andl $-16, %esp subl $112, %esp movl $.LC0, %eax movl %eax, (%esp) call printf movl $0, 108(%esp) jmp .L2 .L3: movl $.LC1, %eax leal 104(%esp), %edx movl %edx, 4(%esp) movl %eax, (%esp) call __isoc99_scanf movl 108(%esp), %eax movl 104(%esp), %edx movl %edx, 84(%esp,%eax,4) addl $1, 108(%esp) .L2: cmpl $4, 108(%esp) jle .L3 movl $.LC2, %eax movl %eax, (%esp) call printf movl $0, 108(%esp) jmp .L4 .L5: movl $.LC1, %eax leal 104(%esp), %edx movl %edx, 4(%esp) movl %eax, (%esp) call __isoc99_scanf movl 108(%esp), %eax movl 104(%esp), %edx movl %edx, 60(%esp,%eax,4) addl $1, 108(%esp) .L4: cmpl $5, 108(%esp) jle .L5 movl $10, 20(%esp) movl $20, 24(%esp) movl $30, 28(%esp) movl $40, 32(%esp) movl $50, 36(%esp) movl $60, 40(%esp) movl $70, 44(%esp) movl $80, 48(%esp) movl $90, 52(%esp) movl $100, 56(%esp) movl $0, 108(%esp) jmp .L6 .L7: movl 108(%esp), %eax leal 1(%eax), %edx movl 108(%esp), %eax movl 84(%esp,%eax,4), %eax movl 60(%esp,%eax,4), %eax movl %eax, 20(%esp,%edx,4) addl $1, 108(%esp) .L6: cmpl $4, 108(%esp) jle .L7 movl $0, 108(%esp) jmp .L8 .L9: movl 108(%esp), %eax movl 20(%esp,%eax,4), %edx movl $.LC3, %eax movl %edx, 8(%esp) movl 108(%esp), %edx movl %edx, 4(%esp) movl %eax, (%esp) call printf addl $1, 108(%esp) .L8: cmpl $9, 108(%esp) jle .L9 leave ret .size main, .-main .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3" .section .note.GNU-stack,"",@progbits