The hardware that we are going to use is PIC32 Starter Kit. The following picture is a screen shot from "PIC32 Starter Kit - DM320001 | Microchip Technology Inc." web site for your information.
In PCSpim, we can use system call (syscall) to handle the input and output. In the hardware that we are using, it is deferent. Let's look at some of the metheds such as how to read integers, how to read a string, how to pring a string and even with values. These are the outcomes of our colaboration with a lab instructor Trevor Douglas in the Faculty of Engineering.
.data prompt1: .asciiz "Please enter an integer:\n\000" in your program, for example, you have la $a0,prompt1 # print prompt on terminal jal printf # output a string
jal readi # read an int and the value is in the $v0
.data printInt: .asciiz "The summing function is: %d\n\000" in your program, you would have the following: la $a0, printInt # print prompt on terminal jal printf you indicate the position for the output data in the message.
.data prompt1: .asciiz "Please enter a string up to 50 chars:\n\000" stringBuffer: .space 50 in your program, you would have the following to read a string: la $a0,prompt1 # print prompt on terminal jal printf # out a string la $a0,stringBuffer # load the address of the stringBuffer li $a1,50 # load the length jal mreads
/********************************************************************* * FileName: sum.S * * Processor: PIC32MX * * Assembler/Compiler/Linker: MPLAB C32 * * * Description: * This project asks the user to enter an integer. The software will * then determine that numbers factorial. * ********************************************************************/ #include################################################# .data prompt1: .asciiz "Please enter an integer:\n\000" printInt: .asciiz "The summing function is: %d\n\000" ## Students DO NOT edit this section ################################################# # # # text segment # # # ################################################# .text .align 2 .globl main .ent main .type main, @function main: .frame $fp,512,$31 # vars= 8, regs= 1/0, args= 0, gp= 8 .mask 0x40000000,-8 .fmask 0x00000000,0 .set noreorder .cpload $v05 .set reorder addiu $sp,$sp,-512 sw $31,508($sp) sw $fp,504($sp) move $fp,$sp .cprestore 16 ## END Students DO NOT edit this section ## Students edit here ################################################# la $a0,prompt1 # print prompt on terminal jal printf # out a string jal readi # read an int move $a0, $v0 # move into the first parameter location jal sumup # call sumup la $a0, printInt # prepare for printing move $a1, $v0 # move the result into $a0 jal printf # printf out the result ## Do not edit below ###################################### move $sp,$fp lw $31,508($sp) lw $fp,504($sp) addiu $sp,$sp,512 j $31 .end main ## Do not edit above ###################################### ############################################################ ## ## The function "sumup" will calculate the sum of ## the integers from 1 to N (N will be passed to ## the function "sumup" by a register $a0, and ## the sum will be returned by $v0). ## ############################################################ sumup: li $t0,0 move $v0,$t0 # Initialize the sum to 0 loop: add $v0, $v0, $a0 # $v0 = $v0 + $a0 addi $a0, $a0, -1 # $a0 = $a0 - 1 bnez $a0, loop # branch to lop if $a0 != 0 jr $ra # return to the calling function
/********************************************************************* * FileName: vowelCount.S * * Processor: PIC32MX * * Assembler/Compiler/Linker: MPLAB C32 * * * Description: * This project asks the user to enter a string. The program then counts the * number of vowels in the string. * ********************************************************************/ #include################################################# .data prompt1: .asciiz "Please enter the string up to 50 chars:\n\000" stringBuffer: .space 50 printNumVowels: .asciiz "The number of vowels is: %d\n\000" #Used for debugging printChar: .asciiz "The var sitting in the return is: %c\n\000" printInt: .asciiz "The int sitting in the return is: %d\n\000" ## Students DO NOT edit this section ################################################# # # # text segment # # # ################################################# .text .align 2 .globl main .ent main .type main, @function main: .frame $fp,512,$31 # vars= 8, regs= 1/0, args= 0, gp= 8 .mask 0x40000000,-8 .fmask 0x00000000,0 .set noreorder .cpload $25 .set reorder addiu $sp,$sp,-512 sw $31,508($sp) sw $fp,504($sp) move $fp,$sp .cprestore 16 ## END Students DO NOT edit this section ## Students edit here ################################################# la $a0,prompt1 # print prompt on terminal jal printf # out a string la $a0,stringBuffer # load the address of the stringBuffer li $a1,40 # load the length jal mreads la $a0, stringBuffer jal vowelCount la $a0, printNumVowels move $a1, $v0 jal printf ## Do not edit below ###################################### move $sp,$fp lw $31,508($sp) lw $fp,504($sp) addiu $sp,$sp,512 j $31 .end main ## Do not edit above ###################################### ############################################################ ## ## The function "sumup" will calculate the sum of ## the integers from 1 to N (N will be passed to ## the function "sumup" by a register $4, and ## the sum will be returned by $2). ## ############################################################ vowelCount: li $s1, 0 move $s0, $a0 loop: lb $s3, ($s0) beq $s3,'a', inc beq $s3,0, exit addi $s0, $s0, 1 j loop inc: addi $s0, $s0, 1 addi $s1, $s1, 1 j loop exit: move $v0, $s1 jr $ra # return to the calling function
Friday, 21-Aug-2020 15:27:44 CST |
|