---
# System prepended metadata

title: MIPS
tags: [110-2學校上課]

---

###### tags: `110-2學校上課`
# MIPS
## 基本參數
1. .word = 整數
2. .asciiz,space,ascii = 文字
3. .float = 經度小數
4. .double = 倍經度小數
## 線段數
```mips=
   .data
    array: .space 404
    tree:.space 1616
    space:.asciiz "\n"
.text
    la $s0,array #$s0 = &array
    la $s1,tree #
    add $t0,$0,1 # i =0
    for1:
        beq $t0,101,endfor1
        sll $t1,$t0,2 # $t1 = $t0*4
        add $t2,$t1,$s0 #$t2 = $t1+$s0
        addi $v0,$0,42
        addi  $a0,$0,0
        addi  $a1,$0,100
        syscall
        sb $a0,0($t2)
        add $t0,$t0,1 # i ++
        j for1
        
    endfor1:    
        addi $a0,$0,1
        addi $a1,$0,1
        addi $a2,$0,4
        jal build
        j endbuild
    build:
        addi $sp,$sp,-16
        sw $a0,($sp)#index    
        sw $a1,4($sp)#left
        sw $a2,8($sp)#right
        sw $ra,12($sp)#ra
        bne $a1,$a2,process
        
        
        sll $a0,$a0,2 
        sll $a1,$a1,2
        
        la $s0,tree
        la $s1,array
        
        add $t0,$s0,$a0#tree[index]
        add $t1,$s1,$a1#array[left]
        lw $t1,($t1)
        sw $t1,($t0)
        
        lw $t0,12($sp)
        addi $sp,$sp,16
        jr $t0
        process:
            lw $t0,($sp)
            lw $t1,4($sp)
            lw $t2,8($sp)
            add $t3,$t1,$t2#left+right
            srl $t3,$t3,1#(left+right)/=2
            sll $t0,$t0,1 # index*2
            
            add $a0,$0,$t0
            add $a1,$0,$t1
            add $a2,$0,$t3
            jal build
            
            lw $t0,($sp)
            lw $t1,4($sp)
            lw $t2,8($sp)
            add $t3,$t1,$t2#left+right
            srl $t3,$t3,1#(left+right)/=2
            addi $t3,$t3,1#(left+right)/=2+1
            sll $t0,$t0,1 # index*2
            addi $t0,$t0,1 # index*2+1
            
            add $a0,$0,$t0
            add $a1,$0,$t3
            add $a2,$0,$t2
            jal build
            
            lw $t0,0($sp)
            lw $t1,4($sp)
            lw $t2,8($sp)
            la $s0,tree
            
            sll $t3,$t0,3
            sll $t4,$t0,3
            addi  $t4,$t4,4
            sll $t5,$t0,2
            
            add $s1,$s0,$t3#tree[2*index]
            add $s2,$s0,$t4#tree[2*index+1]
            add $s3,$s0,$t5#tree[index]
            lw $s1,($s1)
            lw $s2,($s2)
            add $t0,$s1,$s2
            sw $t0,($s3)
            lw $t0,12($sp)
            addi $sp,$sp,16
            jr $t0
    
        
    endbuild:
         la $s1,tree #
        add $t0,$0,1 # i =0
        for2:
            beq $t0,16,Exit
             add $v0,$0,1
            sll $t1,$t0,2 # $t1 = $t0*4
            add $t2,$t1,$s0 #$t2 = $t1+$s0
            lw $a0,0($t2)
            syscall
            
            add $t0,$t0,1 # i ++
            addi $v0, $zero, 4  # print_string syscall
                la $a0, space       # load address of the string
                syscall
            j for2    
    Exit:
			
```




## Read interger and print
```mips=
.data
	istrution:.ascii "Read a interger to print:"

.text
	main:
		li $v0,4
		la $a0,istrution
		syscall
		li $v0,5
		syscall	
		move $a0,$v0
		li $v0,1
		syscall
```

## Read float and print
```mips=
.data
	istrution:.ascii "Read a float to print:"

.text
	main:
		li $v0,4
		la $a0,istrution
		syscall
		li $v0,6
		syscall	
		mov.s $f12,$f0
		li $v0,2
		syscall
```

## Read str and print
```mips=
.data
	istrution:.asciiz "Read a string to print:"
	buffer: .space 8080
.text
	main:
		li $v0,4
		la $a0,istrution
		syscall
		li $v0,8
		la $a0,buffer
		la $a1,100
		syscall	
		li $v0,4
		la $a0,buffer
		syscall
```
## print ramdom nuber
```mips=
.data
	istrution:.ascii "Print a Random(1~100):"

.text
	main:
		li $v0,4# Load print string service
		la $a0,istrution#Load argument 
		syscall
		li $v0,42#Load read interget service
		la $a1,100
		syscall	
		move $a0,$v0#move address $v0 to $a0
		li $v0,1#Load print interget service
		syscall
```
	
	
## Input Output Dialog
```mips=
.data
	istrution:.ascii "Input output dialog "
	str_empty:.asciiz ""
	str_buffer:.asciiz ""
	str_maxsize:.word 100
.text
	main:
		li $v0,4# Load print string service
		la $a0,istrution#Load argument 
		syscall
		li $v0,54#Load Input Dialog service
		la $a0,str_empty
		la $a1,str_buffer
		lw $a2,str_maxsize
		syscall
		li $v0,59
		la $a0,str_empty
		la $a1,str_buffer
		syscall
```
## Array 操作
```mips=
.data
	array:.space 40 # array is a array divide by 4
.text
	li $v0,1
	la $t0,array # $t0 = &array
	li $s0,10 # $s0=10
	sw $s0,0($t0) #$t0 = $s0
	lw $a0,($t0)
	syscall
```
## IF
```
.text 
	li $v0 ,1
	li $t0,10
	li $t1,11
	beq $t0,$t1,else
	la $a0,100
	j exp	
	else: 
	la $a0,99
	exp:
	syscall
		
```
# print 0~99
```mips=
.text 
	li $t0,0
	Loop: 
		li $v0,1
		move $a0,$t0
		syscall
		addi $t0,$t0,1
		bne $t0,100,Loop
			
		
```
## 費式數列
```mips=

.text
	main:
		addi $a0,$0,12
		jal fact
		j exit
	fact:
		addi $sp,$sp,-16
		sw $a0,0($sp)
		sw $ra,4($sp)
		
		bgt $a0,1,re
		add $v0,$a0,$0
		lw $t0,4($sp)
		addi $sp,$sp,16
		jr $t0
		re:	
			lw $t0,($sp)
			addi $a0,$t0,-1
			jal fact
			sw $v0,8($sp)
		
			lw $t0,($sp)
			addi $a0,$t0,-2
			jal fact
			sw $v0,12($sp)
		
			lw $t0,8($sp)
			lw $t1,12($sp)
			add $v0,$t0,$t1
		
			lw $t0,4($sp)
			addi $sp,$sp,16
			jr $t0
		
	exit:
		add $a0,$0,$v0
		addi $v0,$0,1
		syscall

```
```mips
addi $a0,$0,1
		addi $a1,$0,1
		addi $a2,$0,100
		j for2
		jal build
	build:
		addi $sp,$sp,-16
		bne $a1,$a2,process
		
		addi $sp,$sp,16
		sll $a0,$a0,2 
		sll $a1,$a1,2
		
		la $s0,array
		la $s1,tree
		
		add $s0,$s0,$a1#tree[index]
		add $s1,$s1,$a0#array[left]
		sw $s1,($s0)
		
		lw $t0,12($sp)
		jr $t0
		process:
			sw $a0,($sp)#index	
			sw $a1,4($sp)#left
			sw $a2,8($sp)#right
			sw $ra,12($sp)#right
		
			lw $t0,($sp)
			lw $t1,4($sp)
			lw $t2,8($sp)
			add $t3,$t1,$t2#left+right
			srl $t3,$t3,1#(left+right)/=2
			sll $t0,$t0,1 # index*2
			
			lw $a0,($t0)
			lw $a1,($t1)
			lw $a2,($t3)
			jal build
			
			lw $t0,($sp)
			lw $t1,4($sp)
			lw $t2,8($sp)
			add $t3,$t1,$t2#left+right
			srl $t3,$t3,1#(left+right)/=2
			addi $t3,$t3,1#(left+right)/=2+1
			sll $t0,$t0,1 # index*2
			addi $t0,$t0,1 # index*2+1
			
			lw $a0,($t0)
			lw $a1,($t3)
			lw $a2,($t2)
			jal build
			
			lw $t0,0($sp)
			lw $t1,4($sp)
			lw $t2,8($sp)
			la $s0,tree
			
			sll $t3,$t0,2
			sll $t4,$t0,2
			sll $t5,$t0,2
			addi $t4,$t4,1
			
			add $s1,$s0,$t3#tree[2*index]
			add $s2,$s0,$t4#tree[2*index+1]
			add $s3,$s0,$t5#tree[index]
			add $t0,$s1,$s2
			sw $t0,($s3)
			addi $sp,$sp,16
			lw $t0,12($sp)
			jr $t0
	Exit2:
```
## 考試
### BMI
```mips
.data
	input_w :.asciiz "Input Weight" 
	input_h:.asciiz "Input Height" 
.text
	main:
		addi $v0,$0,4
		la $a0,input_w
		syscall
		
		addi $v0,$0,6
		syscall
		mov.s $f3,$f0
		
		addi $v0,$0,4
		la $a0,input_h
		syscall
		
		addi $v0,$0,6
		syscall
		mov.s $f4,$f0
		
		mul.s $f4,$f4,$f4
		div.s $f12,$f3,$f4
		addi $v0,$0,2
		syscall
		#bmi = weight/height^2
```
## 乘法器
```mips=
.data
	A:.word 9487
	B:.word 9487
.text
	addi $t0,$0,1#i
	addi $t1,$0,0#ans
	la $t2,A
	lw $t2,($t2)
	la $t3,B
	lw $t3,($t3)
	For:
		beq $t0,32,EndFor
		andi $t4,$t3,1
		beq $t4,0,Else
		True:
			add $t1,$t1,$t2
			
		Else:
		addi $t0,$t0,1
		srl $t3,$t3,1
		sll $t2,$t2,1
		j For
	EndFor:	
	li $v0,1
	add $a0,$0,$t1			
	syscall
```
## 除法器
```mips= 
.data
	A:.word 9487
	B:.word 3
.text
	addi $t0,$0,1#i
	addi $t1,$0,0#ans
	la $t2,A
	lw $t2,($t2)
	la $t3,B
	lw $t3,($t3)
	la $t7,B
	lw $t7,($t7)
	Align:
		bgt $t3,$t2,End_Align
		sll $t3,$t3,1
		j Align
	End_Align:	
		srl $t3,$t3,1
		
	For:
		beq $t0,32,EndFor
		
		sub $t4,$t2,$t3
		blt $t4,0,Else
		True:
			addi $t1,$t1,1
			sub $t2,$t2,$t3
		Else:
		addi $t0,$t0,1
		srl $t3,$t3,1
		beq $t2,0,EndFor
		blt $t3,$t7,EndFor
		sll $t1,$t1,1
		j For
	EndFor:	
	li $v0,1
	add $a0,$0,$t1			
	syscall
```