MIPS Assembly: Translating C Code, Delayed Loads, and Branches, Schemes and Mind Maps of Computer Programming

Instructions for translating a simple C code snippet into MIPS assembly code. It also explains the concept of delayed loads and branches in MIPS architecture. examples and pseudo instructions for common operations.

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 03/27/2022

hassan-14
hassan-14 🇵🇰

1 document

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Practice
Translate the C code into assembly:
for(i = 0; i < 100; i++)
{
sum+=A[i];
}
Assume
int is 32 bits
$s0 = &A[0]
$v0 = sum;
$t0 = i;
and $t0, $t0, $zero #let i = 0
addi $t1, $zero, 100 #temp = 100
lw $t3, 0($s0) #temp1 = A[i]
add $v0, $v0, $t3 #sum += temp1
addi $s0, $s0, 4 #addr of A[i+1]
addi $t0, $t0, 1 #i = i+1
bne $t1, $t0, LOOP #if i < 100
LOOP:
36
label
1. initialization
2. load A[i] from memory to register
3. add the value of A[i] to sum
4. increase by 1
5. check if i still < 100
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download MIPS Assembly: Translating C Code, Delayed Loads, and Branches and more Schemes and Mind Maps Computer Programming in PDF only on Docsity!

Practice

  • Translate the C code into assembly: for(i = 0; i < 100; i++) { sum+=A[i]; }

Assume

int is 32 bits

$s0 = &A[0]

$v0 = sum;

$t0 = i;

and $t0, $t0, $zero #let i = 0 addi $t1, $zero, 100 #temp = 100 lw $t3, 0($s0) #temp1 = A[i] add $v0, $v0, $t3 #sum += temp addi $s0, $s0, 4 #addr of A[i+1] addi $t0, $t0, 1 #i = i+ bne $t1, $t0, LOOP #if i < 100

LOOP:

label

1. initialization

2. load A[i] from memory to register

3. add the value of A[i] to sum

4. increase by 1

5. check if i still < 100

To implement the following C code, what are the instructions that we should put in the box? A: lw $t0, 0($s2) addi $s2, $s2, 4 C: sw $t0, 0($s2) addi $s2, $s2, 4 B: sw $t1, 0($s2) addi $s2, $s2, 4 D: sw $t1, 0($s2) addi $s2, $s2, 1 add $t0, $zero, $zero addi $t1, $zero, 100 LOOP: addi $t0, $t0, 1 beq $t0, $t1, LOOP for(i = 0; i < 100; i++) A[i] = i;

MIPS Mystery 2: Delayed Branches

  • The instruction after the branch executes even if the branch is taken.
  • All jumps and branches are delayed -- the next instruction always executes Example ori $t0, $zero, 4 beq $t0, $t0, foo ori $t0, $zero, 5 foo: $t0 == 5
file: delayed_branch.s

Why? We’ll talk about it in a few weeks.

Pseudo Instructions

  • Assembly language programming is repetitive
  • Some code is not very readable
  • The assembler provides some simple shorthand for common operations
  • Register $at is reserved for implementing them. Assembly Shorthand Description or $s1, $zero, $s2 mov $s1, $s2 move beq $zero, $zero,

From C to MIPS

Compiling: C to bits

Architecture-

independent

Architecture-

dependent

In the Compiler

Abstract Syntax Tree C-Code

In the Compiler

Abstract Syntax Tree (^) Control Flow Graph

In the Compiler

C-Code Assembly

In the Assembler

Assembly Executable Binary