Wednesday, February 19, 2020

Store values in memory, sort, print in MIPS

This sample MIPS source code will store predefined word values in memory, sort and print them in ascending order. Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.


# ------------------------------------------------ #

.data
szmemoryat: .asciiz "Memory @ "
szLF:       .asciiz "\n"
szTab:      .asciiz ":\t"

.text

# ------------------------------------------------ #
main:

    ori $s0, $zero, 0x1
    sw $s0, 0($gp)
    
    ori $s0, $zero, 0x100
    sw $s0, 4($gp)
    
    ori $s0, $zero, 0x2
    sw $s0, 8($gp)
    
    ori $s0, $zero, 0x1000
    sw $s0, 12($gp)
    
    ori $s0, $zero, 0x23
    sw $s0, 16($gp)
    
    ori $s0, $zero, 0x27
    sw $s0, 20($gp)
    
    ori $s0, $zero, 0x62
    sw $s0, 24($gp)
    
    ori $s0, $zero, 0x37
    sw $s0, 28($gp)
    
    ori $s0, $zero, 0x40
    sw $s0, 32($gp)
    
    ori $s0, $zero, 0x73
    sw $s0, 36($gp)
    
    ori $s0, $zero, 0x47
    sw $s0, 40($gp)
    
    ori $s0, $zero, 0x52
    sw $s0, 44($gp)
    
    ori $s0, $zero, 0x66
    sw $s0, 48($gp)
    
    ori $s0, $zero, 0x59
    sw $s0, 52($gp)
    
    ori $s0, $zero, 0x32
    sw $s0, 56($gp)
    
    ori $s0, $zero , 0x56
    sw $s0, 60($gp)
    
    ori $s0, $zero, 0x68
    sw $s0, 64($gp)
    
    ori $s0, $zero, 0x70
    sw $s0, 68($gp)
    
    ori $s0, $zero, 0x45
    sw $s0, 72($gp)
    
    ori $s0, $zero, 0x81
    sw $s0, 76($gp)
    
    ori $s0, $zero, 0x74
    sw $s0, 80($gp)
    
    or $s0, $zero, $gp
    ori $s5, $zero, 0x15
    ori $s1, $zero, 0xFFFF
    sll $s1, $s1, 16
    ori $s1, $s1, 0xFFFF
    
    or $s2, $zero, $gp              #First pointer
    or $s3, $zero, $gp              #Last pointer
    
    addi $s2, $s2, -4
# ------------------------------------------------ #
outer_loop:

    addi $s2, $s2, 4
    and $s3, $s1, $s2
    
    addi $s3, $s3, 4
    and $s4, $s5, $s1
    
    
    addi $s5, $s5, -1
    addi $s4, $s4, -1
    
    beq $s5, $zero, end
# ------------------------------------------------ #
inner_loop:

    lw $t0, 0($s2)
    lw $t1, 0($s3)
    
    slt $t3, $t0, $t1
    
    beq $t3, $zero, outer_less
    
    addi $s3, $s3, 0x4
    addi $s4, $s4, -1
    
    beq $s4, $zero, outer_loop
    
    beq $zero, $zero, inner_loop
# ------------------------------------------------ #
outer_less:

    sw $t1, 0($s2)
    sw $t0, 0($s3)
    
    addi $s3, $s3, 0x4
    addi $s4, $s4, -1
    
    beq $s4, $zero, outer_loop
    
    beq $zero, $zero, inner_loop

end:
# ------------------- Print --------------------- #
    # Print all memory locations
    li $a3, 0
    add $a1, $gp, $a3
    li $s0, 80
    
    next_location:
    # Print "Memory @ "
    li $v0, 4
    la $a0, szmemoryat
    syscall
    
    # Print Memory location in Hex
    li $v0, 34
    move $a0, $a1
    syscall
    
    # Print tab
    li $v0, 4
    la $a0, szTab
    syscall
    
    # Load integer in memory and print it in decimal
    li $v0, 1
    lw $a0, ($a1)
    syscall
    
    # Print newline
    li $v0, 4
    la $a0, szLF
    syscall
    
    addi $a3, $a3, 4
    add $a1, $gp, $a3
    ble $a3, $s0, next_location
# ------------------------------------------------ #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment