Computer architecture and assembly language lab 2 Printing
Computer architecture and assembly language lab 2
Printing byte character
Printing byte integer
Printing word integer
Printing float number
Printing double
Addition two integers
Subtracting two integers
Multiplication two integers mul instruction
Multiplication tow integers mult instruction
Reading and Printing an Integer ######### Code segment ###########. text. globl main: # main program entry li $v 0, 5 # Read integer syscall # $v 0 = value read move $a 0, $v 0 li $v 0, 1 syscall # $a 0 = value to print # Print integer li $v 0, 10 syscall # Exit program
Reading and Printing a String ######### Data segment ###########. data str: . space 10 # array of 10 bytes ######### Code segment ###########. text. globl main: # main program entry la $a 0, str # $a 0 = address of str li $a 1, 10 # $a 1 = max string length li $v 0, 8 # read string syscall li $v 0, 4 # Print string str syscall li $v 0, 10 # Exit program syscall
Program 1: Sum of Three Integers # Sum of three integers # # Objective: Computes the sum of three integers. # Input: Requests three numbers. # Output: Outputs the sum. ########## Data segment ##########. data prompt: . asciiz "Please enter three numbers: n" sum_msg: . asciiz "The sum is: " ########## Code segment ##########. text. globl main: la $a 0, prompt # display prompt string li $v 0, 4 syscall li $v 0, 5 # read 1 st integer into $t 0 syscall move $t 0, $v 0
Sum of Three Integers – Slide 2 of 2 li $v 0, 5 syscall move $t 1, $v 0 # read 2 nd integer into $t 1 li $v 0, 5 syscall move $t 2, $v 0 # read 3 rd integer into $t 2 addu # accumulate the sum $t 0, $t 1 $t 0, $t 2 la $a 0, sum_msg li $v 0, 4 syscall # write sum message move $a 0, $t 0 li $v 0, 1 syscall # output sum li $v 0, 10 syscall # exit
Sll muliplication Supported by MARS
Integer division
Branching instruction. data message: . asciiz " the numbers are different" message 1: . asciiz " numbers are equal". text main: addi $t 0, $zero, 61 addi $t 1, $zero , 61 bne $t 0, $t 1, numbersdifferent beq $t 0, $t 1, numbersequal li $v 0, 10 syscall
numbersdifferent: li $v 0, 4 la $a 0, message syscall li $v 0, 10 syscall numbersequal: li $v 0, 4 la $a 0, message 1 syscall li $v 0, 10 syscall
Introduction to function
OUTLINES • If statement • Function pass value • While loop
If statemnt if (($sl > $s 2) || ($s 2 > $s 3)) {$s 4 = 1; }
If statement if (($s 3 <= $s 4) && ($s 4 > $s 5)) { $s 3 = $s 4 + $s 5 }
Function pass value
While loop
Quiz 1 • Write a mips program to solve the following equation by using while loop? • sum= 2+4+6+…+10
- Slides: 25