fpga4student.com FPGA digital design projects using Verilog/ VHDL: MIPS assembly dynamically allocating memory example

Thursday, November 3, 2016

MIPS assembly dynamically allocating memory example

MIPS Assembly Dynamically Allocating memory example:


Enter player's name, points and minutes, and then sorting using dynamically allocating techniques. 
Expected results:

MIPS assembly dynamically allocating memory

MIPS Assembly Code:

.data
    buffer: .space 28
done_str: .asciiz "DONE"
enter_players_name: .asciiz    "Enter player's last name:           "
enter_players_points: .asciiz  "Enter player's points per game:     "
enter_players_minutes: .asciiz "Enter player's minutes per game:    "
newline: .asciiz "\n"
blank_text: .asciiz "         " 
.text
 main:   addi $s4,$zero,0 # count number of players 
 loop_in:la $a0,enter_players_name
li $v0, 4 
syscall # load and print asking for player's last name
li $v0, 8 # take the last name of the player 
la $a0,buffer 
li $a1,28
syscall
# count number of characters in the last name 
la $t1, buffer # lower array pointer = array base
la $t2, buffer # start upper pointer at beginning
LengthLp:
lb $t3, ($t2) # grab the character at upper ptr
beqz $t3, LengthDn # if $t3 == 0, we're at the terminator
addi $t2, $t2, 1 # count the character
b LengthLp # repeat the loop
LengthDn:
addi $t2, $t2, -1  
sub $s7,$t2,$t1 # number of characters are entered
addi $t3,$zero,4
beq $s7,$t3,compDone # compare with "DONE" to finish the program 
loop_i1:la $a0,enter_players_points
li $v0, 4 
syscall # load and print asking for player's points per game 
li $v0, 6 # read float 1
syscall # do it
mov.s $f1, $f0
la $a0,enter_players_minutes
li $v0, 4 
syscall # load and print asking for player's minutes per game
li $v0, 6 # read float 2
syscall # do it
mov.s $f2, $f0
mtc1 $zero, $f3 #move to FP registers
cvt.s.w $f4, $f3
c.eq.s $f1, $f4
bc1t zero_float
c.eq.s $f2, $f4
bc1t zero_float
div.s $f5, $f1, $f2
#--------------DONE ASKING--------## $s4: number of players
zero_back: beq $s4,$zero,first_player
addi $s4,$s4,1# Add new players and allocate new memory, deallocate the old memory 
addi $t3,$zero,4
addi $t4,$zero,28
mult $s4,$t4
mflo $t5 # new number of bytes of name memory 
mult $s4,$t3
mflo $t6 # new number of bytes of point/min memory 
li  $v0,9      # Allocate new memory for name memory
move $a0,$t5 
syscall        # $v0 <= address of name memory 
move $s1,$v0 # new address of name memory
li  $v0,9     # Allocate new memory for point/min memory 
move $a0,$t6
syscall        # $v0 <= address of point/min memory 
move $s2,$v0 #new address  of point/min memory
#---Copy data from old mem to new mem --#
# $s4-1: number of players in old mem 
# $s5: address of old name memory 
# $s6: address of old point/min memory 
# $s1: address of new name memory 
# $s2: address of new point/min memory 
addi $s7,$zero,0 
loop_copy:
addi $t3,$zero,28
mult $s7,$t3
mflo $t4
addi $t5,$zero,4
mult $s7,$t5
mflo $t6
add $t0,$t4,$s5 # copy names 
add $t1,$t4,$s1
add $t2,$t6,$s6 # copy point/min 
add $t3,$t6,$s2 
# change number of characters
addi $t7,$zero,0
loop1:  add $s0,$t0,$t7
add $t4,$t1,$t7
lw $t5,0($s0) # copy name of the player 
sw $t5,0($t4)
addi $t7,$t7,4
addi $t5,$zero,28
bne $t7,$t5,loop1
l.s $f3,0($t2)
s.s $f3,0($t3) # copy point/min of the player 
addi $s7,$s7,1
addi $t0,$s4,-1
beq $s7,$t0,save_new_player #save until the end of the old memory 
j loop_copy
save_new_player:
addi $t0,$s4,-1
addi $t3,$zero,28
mult $t0,$t3
mflo $t4
addi $t5,$zero,4
mult $t0,$t5
mflo $t6
la $a0,buffer
add $t1,$t4,$s1
add $t3,$t6,$s2 
# change number of characters
addi $t7,$zero,0
loop2:  add $s0,$a0,$t7
add $t4,$t1,$t7
lw $t5,0($s0) # copy name of the player 
sw $t5,0($t4)
addi $t7,$t7,4
addi $t5,$zero,28
bne $t7,$t5,loop2
s.s $f5,0($t3) # save point/min of the player 
#deallocate the old memory 
move $a0,$s5 # address of old memory 
addi $t5,$zero,28
addi $t6,$s4,-1 
mult $t6,$t5 
mflo $t6 
move $a1, $t6 # number of bytes of old memory
jal zeroMem
move $a0,$s6  # address of old memory 
addi $t5,$zero,4 
addi $t6,$s4,-1 
mult $t6,$t5 
mflo $t6 
move $a1, $t6 # number of bytes of old memory
jal zeroMem
move $s5,$s1 #update old memory of name 
move $s6,$s2 #update old memory of point/min 
j loop_in
first_player:
li  $v0,9      # Allocate a block of memory for name array
li $a0,28 
syscall        # $v0 <= address of name array 
move $s5,$v0 
li  $v0,9     # Allocate a block of memory for value array 
li $a0,4 
syscall        # $v0 <= address of value array 
move $s6,$v0
#--save data--#
la $a0,buffer
# change number of characters
addi $t7,$zero,0
loop3:  add $s0,$a0,$t7
add $t4,$s5,$t7
lw $t5,0($s0) # copy name of the player 
sw $t5,0($t4)
addi $t7,$t7,4
addi $t5,$zero,28
bne $t7,$t5,loop3
s.s $f5,0($s6)
addi $s4,$s4,1
j loop_in 
.........
To be continued


Source code: comment your email below
You might like this:

You might also like this:
VHDL code for D Flip Flop
Verilog code for D Flip Flop
Verilog code for a comparator
Verilog code for FIFO memory
VHDL code for FIFO memory

No comments:

Post a Comment