yearlyBalance that uses a given interest rate, balance, and term to output a table of adjusted balances each year for the number of years specified by the term. For this lab you will use a simplified formula:
interest = balance * rate / 100.0;
ANSWER
To create the yearlyBalance
procedure in assembly language, you’ll need to follow these steps:
- Create a file named
balances.asm
to define theyearlyBalance
procedure. - In the
balances.asm
file, define theyearlyBalance
procedure according to your requirements. - Create a separate file named
main.asm
to read user input and call theyearlyBalance
procedure. - In the
main.asm
file, include the necessary code to read user input and call theyearlyBalance
procedure. - Assemble both
balances.asm
andmain.asm
into an executable program.
Here’s an example implementation in x86 assembly language (assuming you’re using the x86 architecture and NASM assembler):
balances.asm
:
section .text
global yearlyBalance
yearlyBalance:
; Inputs:
; - [esp+4]: Initial balance
; - [esp+8]: Interest rate (float)
; - [esp+12]: Number of years
; Output:
; - Print the adjusted balances each year
push ebp
mov ebp, esp
; Load initial values
fld dword [ebp + 8] ; Load balance onto FPU stack
fild dword [ebp + 12] ; Load the number of years as an integer
fdiv ; Divide balance by years
; Loop through the years
mov eax, 1 ; Initialize the year counter
jmp startLoop
loopBegin:
fld dword [ebp + 8] ; Reload balance onto FPU stack
fmul ; Multiply balance by rate
fdiv dword [ebp + 16] ; Divide by 100.0
fadd ; Add interest to balance
startLoop:
; Print the result
push eax
push ebx
push ecx
push edx
pushf
mov eax, 4 ; syscall number for sys_write
mov ebx, 1 ; file descriptor (stdout)
mov ecx, esp ; pointer to the string to print
mov edx, 16 ; length of the string
mov edx, eax
fwait
fstp qword [esp]
mov eax, 1
mov ebx, 0
int 80h
popf
pop edx
pop ecx
pop ebx
pop eax
; Increment the year counter
inc eax
cmp eax, dword [ebp + 12] ; Compare with the number of years
jle loopBegin ; Jump back to the beginning of the loop if not done
; Clean up and return
pop ebp
ret
main.asm
:
section .data
prompt_balance db "Enter your balance: ", 0
prompt_rate db "Enter the interest rate (like 5.0): ", 0
prompt_years db "Enter the number of years: ", 0
output_format db "Year: %d: $%.2f", 10, 0
section .bss
balance resq 1
rate resq 1
years resq 1
section .text
global _start
extern yearlyBalance
_start:
; Read initial balance
mov eax, 4
mov ebx, 1
mov ecx, prompt_balance
mov edx, 26
int 80h
mov eax, 3
mov ebx, 0
mov ecx, balance
mov edx, 8
int 80h
; Read interest rate
mov eax, 4
mov ebx, 1
mov ecx, prompt_rate
mov edx, 31
int 80h
mov eax, 3
mov ebx, 0
mov ecx, rate
mov edx, 8
int 80h
; Read number of years
mov eax, 4
mov ebx, 1
mov ecx, prompt_years
mov edx, 24
int 80h
mov eax, 3
mov ebx, 0
mov ecx, years
mov edx, 8
int 80h
; Call yearlyBalance procedure
fld dword [balance]
fld dword [rate]
fld dword [years]
call yearlyBalance
; Exit the program
mov eax, 1
mov ebx, 0
int 80h
To assemble and link these files, you can use the following commands (assuming you’re on a Linux system):
nasm -f elf balances.asm -o balances.o
nasm -f elf main.asm -o main.o
ld -m elf_i386 -s -o yearly_balance main.o balances.o
This will create an executable file named yearly_balance
. When you run it, it will prompt you for input and then display the adjusted balances for each year.
QUESTION
Description
Create a procedure called yearlyBalance that uses a given interest rate, balance, and term to output a table of adjusted balances each year for the number of years specified by the term. For this lab you will use a simplified formula:
interest = balance * rate / 100.0;
Pass the initial balance and rate and number of years to the procedure on the floating point stack. Then, using a for loop output the table. Basically you want to add the interest to the balance each year (each time through the loop)
Required:
The yearlyBalance procedure must be in a separate asm file called balances.asm. This means you should have main.asm and balances.asm. You can format these as you normally do and paste them one after the other in the submissions box.
The following shows an example output:
Enter your balance
1000
Enter the interest rate (like 5.0)
5.0
Enter the number of years
10
Year: 1: $1050.00
Year: 2: $1102.50
Year: 3 $1157.63
Year: 4: $1215.51
Year: 5: $1276.28
Year: 6: $1340.10
Year: 7: $1407.10
Year: 8: $1477.46
Year: 9: $1551.33
Year: 10: $1628.89
Press any key to continue . . .