Wednesday, May 20, 2020

MIPS Heap Memory

This is a sample MIPS program that allocates heap memory and uses it to store several values entered by the user. The code is fully documented.

Launch EzMIPSthe MIPS assembler simulator, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.

# ------ allocate heap memory and use it for storage in MIPS ----- #

.data
szEnterID:    .asciiz "Enter ID: "
szID:         .asciiz "ID: "

szEnterYear:  .asciiz "Enter Year: "
szYear:       .asciiz "Year: "

szEnterTitle: .asciiz "Enter Title: "
szTitle:      .asciiz "Title: "

szEnterNotes: .asciiz "Enter Notes: "
szNotes:      .asciiz "Notes: "

szValues:     .ascii "\n"
              .ascii  "Values stored in Heap memory:\n"
              .asciiz "-----------------------------\n"

szLF:         .asciiz "\n"

.text
# ................. Allocate heap memory of 384 bytes ............ #

    li $v0, 9            # allocate heap memory code
    li $a0, 384          # How much memory (in bytes) = 384
    syscall              # allocate it!
    move $s0, $v0        # store the address of our allocated memory
                         # in $s0

# ................................................................ #

    # print "Enter ID: "
    li $v0, 4            # print string code
    la $a0, szEnterID    # load address of szEnterID
    syscall              # print it!

    # Read integer 'ID' from user
    li $v0, 5            # read integer syscall code
    syscall              # do it!
    sw $v0, 0($s0)       # store ID into memory Offset: 0

# ................................................................ #

    # print "Enter Year: "
    li $v0, 4            # print string code
    la $a0, szEnterYear  # load address of szEnterYear
    syscall              # print it!

    # Read integer 'Year' from user
    li $v0, 5            # read integer syscall code
    syscall              # do it!
    sw $v0, 4($s0)       # store year into memory Offset: 4

# ................................................................ #

    # print string "Enter Title: "
    li $v0, 4            # print string code
    la $a0, szEnterTitle # load address of szEnterTitle
    syscall              # print it!

    # Read string 'Title' from user
    li $v0, 8            # read string
    addiu $a0, $s0, 8    # store 'Title' into memory Offset: 8
    li $a1, 64           # $a1 = maximum number of characters to read
    syscall

# ................................................................ #

    # print "Enter Notes: "
    li $v0, 4            # print string code
    la $a0, szEnterNotes # load address of szEnterNotes
    syscall              # print it!

    # Read string 'Notes' from user
    li $v0, 8            # read string
    addiu $a0, $s0, 72   # store 'Notes' into memory Offset: 72
    li $a1, 256          # $a1 = maximum number of characters to read
    syscall              # do it!

# ................................................................ #

    # print "Values read & stored in Heap memory:"
    #       "------------------------------------"
    li $v0, 4            # print string code
    la $a0, szValues     # load address of szValues
    syscall              # print it!

# ............................. ID ............................... #
    
    # print "ID: "
    li $v0, 4            # print string code
    la $a0, szID         # load address of szID
    syscall              # print it!


    # print integer 'ID'
    li $v0, 1            # print integer code
    lw $a0, 0($s0)       # Print the ID stored at $s0     [Offset: 0]
    syscall              # print it!
    
    # print "\n"
    li $v0, 4            # print string code
    la $a0, szLF         # load address of szLF
    syscall              # print it!

# ............................. Year ............................. #
    
    # print "Year: "
    li $v0, 4            # print string code
    la $a0, szYear       # load address of szYear
    syscall              # print it!
    
    # print integer 'Year'
    li $v0, 1            # print integer code
    lw $a0, 4($s0)       # Print the Year stored at $s0   [Offset: 4]
    syscall              # print it!
    
    # print "\n"
    li $v0, 4            # print string code
    la $a0, szLF         # load address of szLF
    syscall              # print it!

# ............................. Title ............................ #
    
    # print "Title: "
    li $v0, 4            # print string code
    la $a0, szTitle      # load address of szTitle
    syscall              # print it!

    # print string 'Title'
    li $v0, 4            # print string code
    addiu $a0, $s0, 8    # Print the Title stored at $s0 [Offset: 8]
    syscall              # print it!

    # print "\n"
    li $v0, 4            # print string code
    la $a0, szLF         # load address of szLF
    syscall              # print it!

# ............................. Notes ............................ #
    
    # print "Notes: "
    li $v0, 4            # print string code
    la $a0, szNotes      # load address of szNotes
    syscall              # print it!

    # print string 'Notes'
    li $v0, 4            # print string code       
    addiu $a0, $s0, 72   # Print description stored at $s0 [Offset: 72]
    syscall              # print it!

    # print "\n"
    li $v0, 4            # print string code
    la $a0, szLF         # load address of szLF
    syscall              # print it!

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

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

Regards,

Antonis

No comments:

Post a Comment