Sunday, February 23, 2020

strcpy() implementation in MIPS

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

# -------------- strcpy() implementation in MIPS ------------- #

.data


str_source:  .asciiz "Hello There!\n"

str_target:  .space 20

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


.text


# ----------- load source and target string addresses -------- #


    la $a1, str_source

    la $a0, str_target
    jal strcpy                   # call strcpy function

# ---------------------------- print ------------------------- #


    la $a0, str_target           # address of string

    li $v0, 4                    # print string
    syscall

# -------------------------- exit program -------------------- #


    li $v0, 10

    syscall

# ---------------------------- strcpy() ---------------------- #


strcpy:

    lb $t1, 0($a1)
    sb $t1, 0($a0)
    beq $t1, $zero, return
    addi $a0, $a0, 1
    addi $a1, $a1, 1
    j strcpy

return:
    jr $ra

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


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

Regards,

Antonis

No comments:

Post a Comment