Behold the MIPS machine MIPS Memory � The assembly language level (2 32 -1) � . . . MIPS private regs � PC � (A+24) � Introduction to MIPS IR � (A+20) � MIPS user regs � (A+16) � 0 0 0 0 $zero � (A+12) � $v0 � (A+8) � . . . (A+4) � $a0 � . . . (A) � $t0 � CS240 Computer Organization . . . Department of Computer Science $t1 � (12) � Wellesley College . $t2 � MIPS � (8) � . . . (4) � $s0 � (0) � $s1 � . . . MIPS 2-2 Our second assembly program Assembler directives ############################################################## � ############################################################## � # addTwoNumbers.asm � � � � � � # � # addTwoNumbers.asm � � � � � � # � # This program computes the sum of two numbers � � # � # This program computes the sum of two numbers � � # � # X and Y that are input during program execution. � � # � # X and Y that are input during program execution. � � # � ############################################################### � ############################################################## � # program instructions � � � �� � � .text � � � � # program instructions � � � � � � # read 1st num into location X � � � .globl main � � �� � � � � � # read 2nd num into location Y � main: � � � � � # input X � � � �� � � � � � � # input Y � � � � � � # add Y to X and store in AC � �� � � � � � # put the sum into location SUM � � � � � � � # add X to Y and � � � � � � # write SUM to screen � � � � � � � # store SUM � � � � � � # and stop � � � � � � � # output SUM �� # program variables � � � � �� � � � � � � # halt � � � � �� � � .data � � � � # program data �� MIPS 2-3 MIPS 12 1 ¡
Well, plenty of registers … But where should X, Y and SUM go? MIPS Memory � (2 32 -1) � . . . ############################################################## � MIPS private regs � … so assign X to $t0, # addTwoNumbers.asm � � � � � � # � PC � (A+24) � Y to $t1, and # This program computes the sum of two numbers � � # � IR � SUM to $t2 (A+20) � # X and Y that are input during program execution. � � # � MIPS user regs � (A+16) � ############################################################## � 0 0 0 0 $zero � � � .text � � � � # program instructions � (A+12) � � � .globl main � � �� $v0 � (A+8) � . . . main: � � � � � # input X � (A+4) � $a0 � � � � � � � # input Y � . . . �� (A) � X $t0 � . . . � � � � � � # add X to Y and � Y � � � � � � # store SUM � $t1 � (12) � � � � � � � # output SUM �� . SUM $t2 � MIPS � (8) � . . . � � � � � � # halt � (4) � � � .data � � � � # program data �� $s0 � (0) � $s1 � . . . MIPS 2-5 MIPS 2-6 How do we input values to X and Y? First reserve room for prompt ############################################################## � ############################################################## � # addTwoNumbers.asm � � � � � � # � # addTwoNumbers.asm � � � � � � # � # This program computes the sum of two numbers � � # � # This program computes the sum of two numbers � � # � # X and Y that are input during program execution. � � # � # X and Y that are input during program execution. � � # � ############################################################## � ############################################################## � � � .text � � � � # program instructions � � � .text � � � � # program instructions � � � .globl main � � �� � � .globl main � � �� main: � � � � � # input X � main: � � � � � # input X � � � � � � � # input Y � � � � � � � # input Y � � � � � � � �� � � � � � � �� � � � � � � # add X to Y and � � � � � � � # add X to Y and � � � � � � � # store SUM � � � � � � � # store SUM � Another assembler � � � � � � # output SUM �� � � � � � � # output SUM �� directive � � � � � � # halt � � � � � � � # halt � � � .data � � � � # program data �� � � .data � � � � # program data � str: .asciiz “Enter number: “ �� MIPS 2-7 MIPS 2-8 2 ¡
str: Reading in an integer � MIPS Memory � .asciiz “Enter number: “ � (2 32 -1) � . . . MIPS private regs � ############################################################## � # addTwoNumbers.asm � � � � � � # � PC � (A+24) � # We’re going need some more room up here real soon. � # � IR � (A+20) � ############################################################## � MIPS user regs � (A+16) � � � .text � � � � # program instructions � 0 0 0 0 $zero � 0 : (A+12) � � � .globl main � � �� Pseudoinstructions main: � $v0 � (A+8) � r e b m . . . � � li � $v0, 4 � � � # sys call code print_str � u n r (A+4) � $a0 � � � la � $a0, str � � # addr of string to print � . . . str e t n E (A) � � � syscall � � � � # print the prompt � X $t0 � . . . � � � � � � # input x; input Y � Okay OS do your Y $t1 � � � � � � � # add X to Y and � (12) � thing . � � � � � � # store SUM � SUM $t2 � MIPS � (8) � . . . � � � � � � # output SUM �� (4) � $s0 � � � � � � � # halt � � � .data � � � � # program data � (0) � $s1 � . . . str: .asciiz “Enter number: “ �� MIPS 2-9 MIPS 2-10 li � $v0, 4 la � $a0, str syscall � MIPS Memory � MIPS Memory � � (2 32 -1) � (2 32 -1) � MIPS private regs � . . . MIPS private regs � . . . PC � PC � (A+24) � (A+24) � IR � IR � (A+20) � (A+20) � MIPS user regs � (A+16) � MIPS user regs � (A+16) � 0 0 0 0 0 0 0 0 $zero � $zero � (A+12) � 0 : (A+12) � 0 : 0 0 0 4 0 0 0 4 $v0 � r e b m $v0 � r e b m (A+8) � (A+8) � . . . . . . u n r u n r 0 0 0 A (A+4) � 0 0 0 A (A+4) � $a0 � $a0 � . . . . . . Enter number: � str e t n E str e t n E (A) � (A) � X X $t0 � $t0 � . . . . . . Y Y $t1 � $t1 � (12) � (12) � . . SUM $t2 � SUM $t2 � MIPS � (8) � MIPS � (8) � . . . . . . (4) � (4) � $s0 � $s0 � $s1 � (0) � $s1 � (0) � . . . . . . MIPS 2-11 MIPS 2-12 3 ¡
li � $v0, 5 Reading in X � MIPS Memory � syscall � (2 32 -1) � . . . � � .text � � � � # program instructions � MIPS private regs � � � .globl main � � �� PC � (A+24) � main: � IR � (A+20) � � � li � $v0, 4 � � � # sys call code print_str � MIPS user regs � (A+16) � � � la � $a0, str � � # addr of string to print � 0 0 0 0 $zero � 0 : � � syscall � � � � # print the prompt �� (A+12) � � � li � $v0, 5 � � � # sys code read integer � 0 0 0 5 $v0 � (A+8) � r e b m . . . � � syscall � u n r 0 0 0 A (A+4) � $a0 � � � move � $t0, $v0 � � # $t0 is the variable X � . . . Enter number: � str e t n E � � � � � � # input Y � (A) � X $t0 � . . . � � � � � � # add X to Y and � Y � � � � � � # store SUM � $t1 � (12) � � � � � � � # output SUM �� . SUM $t2 � MIPS � (8) � . . . � � � � � � # halt � (4) � � � .data � � � � # program data � $s0 � str: .asciiz “Enter number: “ �� (0) � $s1 � . . . MIPS 2-13 MIPS 2-14 The user enters 3 move $t0, $v0 � MIPS Memory � MIPS Memory � (2 32 -1) � (2 32 -1) � … and it MIPS private regs � . . . MIPS private regs � . . . miraculously PC � PC � appears (A+24) � Copy the contents (A+24) � in register $v0 of $v0 to $t0 IR � IR � (A+20) � (A+20) � MIPS user regs � (A+16) � MIPS user regs � (A+16) � 0 0 0 0 0 0 0 0 $zero � $zero � (A+12) � 0 : (A+12) � 0 : 0 0 0 3 0 0 0 3 $v0 � r e b m $v0 � r e b m (A+8) � (A+8) � . . . . . . u n r u n r 0 0 0 A (A+4) � 0 0 0 A (A+4) � $a0 � $a0 � . . . . . . Enter number: 3 � Enter number: 3 � str e t n E str e t n E (A) � (A) � X X 0 0 0 3 $t0 � $t0 � . . . . . . Y Y $t1 � $t1 � (12) � (12) � . . SUM $t2 � SUM $t2 � MIPS � (8) � MIPS � (8) � . . . . . . (4) � (4) � $s0 � $s0 � $s1 � (0) � $s1 � (0) � . . . . . . MIPS 2-15 MIPS 2-16 4 ¡
Recommend
More recommend