<p style="font-size: 14px;text-align:center">
The Islamic University of Gaza
</p>
<p style="font-size: 14px;text-align:center">
Computer Architecture Lab - 2023
</p>
<p style="color:#000033;font-weight: bold;text-align:center">
LAB (1) || Introduction to Assembly Language
</p>
<p style="text-align:right">
Eng: Amal I. Mahfouz , Eng: Hassan Ghazy
</p>
****
<p style="color:#000066;font-weight: bold;">
objective
</p>
1. what is computer system architecture?
2. what is classify of Computer Languages?
3. write a simple program in assembly language.
4. convert high level code to assembler code.
---
<p style="font-size:20px;color:#000066;font-weight: bold;">
Computer System Architecture
</p>
A computer system is basically a machine that simplifies complicated tasks. It should maximize performance and reduce costs as well as power consumption.The different components in the Computer System Architecture are:
* Input Unit
* Output Unit
* Storage Unit
* Arithmetic Logic Unit
* Control Unit ,etc.
A diagram that shows the flow of data between these units is as follows :

The input data travels from input unit to ALU. Similarly, the computed data travels from ALU to output unit. The data constantly moves from storage unit to ALU and back again. This is because stored data is computed on before being stored again. The control unit controls all the other units as well as their data
---
<p style="font-size:20px;color:#000066;font-weight: bold;">
Computer Languages
</p>
Over the years, computer languages have been evolved from Low-Level to High-Level Languages. In the earliest days of computers, only Binary Language was used to write programs.
The computer languages are classified as follows:

e.g. :

---
<p style="font-size:20px;color:#000066;font-weight: bold;">
Assembly Language
</p>
An assembly language is a type of middle-level programming language that is intended to communicate with a computer’s hardware. Unlike machine language, which consists of binary and hexadecimal characters, assembly languages are designed to be readable by humans.

---
<p style="font-size:20px;color:#000066;font-weight: bold;">
Registers:
</p>
Registers are a type of computer memory used to quickly accept, store, and transfer data and instructions that are being used immediately by the CPU.

For example what instructions it has and what the instructions do.
MIPS is a great language to learn the basics of assembly programming.
<p style="font-size:17px;color:#000066;font-weight: bold;">
MIPS Instruction Set:(operation)
</p>
Arithmetic, Logical, Data Transfer, Conditional Branch, and Comparison Instructions.
A MIPS instruction is 32 bits (always)
* <p style="font-size:16px;color:#4d0000;font-weight: bold;">
Data Transfer
</p>
The MIPS instruction that loads a word from memory into a register is the **lw** instruction.The store word instruction is **sw** .

***Memory Instructions Format***
Lw $ destination register's address, offset($ source register's address).
SW $source register's address, offset($destination register's address)
---
* <p style="font-size:16px;color:#4d0000;font-weight: bold;">
Logical Instructions
</p>

e.g.:
* **and $s1, $s2, $s3** (this mean: $s1 = $s2 & $s3 Bitwise AND)
* **or $s1, $s2, $s3** (this mean: $s1 = $s2 | $s3 Bitwise OR)
* **sll $s1, $s2, 10** (this mean: $s1 = $s2 <<10 Shift left by constant number of bits)
* **srl $s1, $s2, 10** (this mean: $s1 = $s2 >>10 Shift right by constant number of bits)
---
* <p style="font-size:16px;color:#4d0000;font-weight: bold;">
Arithmetic Instructions
</p>

rs: 1st register operand (register source)
rt: 2nd register operand
rd: register destination
**SUB (Subtract)** : **sub rd, rs ,rt**
<p style="font-size:16px;color:#b30000;font-weight: bold;">
Example:
</p>
Converting High-level Programming Concepts to Assembly
**f = (g+h) - (i+j)**
* assume f in $s2, g in $s4, h in $s5, i in $s6, and j in $s7
*Mips assembly:*
add $s1, $s4, $s5
add $s3, $s6, $s7
sub $s2, $s1, $s3
or :
add $s2, $s4, $s5
sub $s2, $s2, $s6
sub $s2, $s2, $s7
**ps:**
1. instructions are simple: fixed number of operands(unlike C language ).
1. a single line of C code is converted into multiple lines of assembly code.
2. some sequences are better than other.
---
<p style="font-size:16px;color:#4d0000;font-weight: bold;">
Immediate Operands
</p>
When an instruction requires two operands, the first operand is generally the destination, which contains data in a register or memory location and the second operand is the source. Source contains either the data to be delivered (immediate addressing) or the address (in register or memory) of the data.
An immediate instruction uses a constant number as one of the inputs (insted of a register operand) .

<p style="font-size:16px;color:#b30000;font-weight: bold;">
Example:
</p>
convert to assembly:
c code : **d[3] = d[2] + a**
**assume d[0] in $s4, and a in $s1**
ps: A MIPS instruction is 32 bits (always) , 4 byte
lw $t0, 8($s4) # d[2]
lw $t1, 0($s1) # a
add $t0, $t0, $t1 # $t0 = d[2]+a
sw $t0, 12($s4) # store sum in d[3]
---
<p style="font-size:16px;color:#4d0000;font-weight: bold;">
Control Instructions
</p>
1. **Conditional Branch** : branch on equal (beq), branch on not equal (bne)
**beq register1, register2 , label instruction**
this mean : jump to label instruction if register1 equals to register2
1. **Comparison** : set on less than (slt),set less than immediate. (slti)
**slt $s1, $s2, $s3**
*If true, set $s1 to 1. Otherwise, set $s1 to 0*.
1. **Unconditional Branch**: jump (j), jump register (jr)
<p style="font-size:16px;color:#b30000;font-weight: bold;">
Example:
</p>
assume: f in $s0, i,j in $s3, $s4 , and g,h in $s1, $s2

---
<p style="font-size:16px;color:#b30000;font-weight: bold;">
Example:
</p>

---
<p style="font-size:16px;color:#b30000;font-weight: bold;">
Example:
</p>

---
<p style="font-size:16px;color:#b30000;font-weight: bold;">
Task(1):
</p>

---
<p style="font-size:16px;color:#b30000;font-weight: bold;">
Task(2):
</p>
convert to assembly code:

---
<p style="font-size:16px;color:#b30000;font-weight: bold;">
Task(3):
</p>
convert to assembly code:
**Switch (k){
case 0 : f = i + j; break;
case 1 : f = g + h; break;
case 2 : f = g -h; break;
case 3 : f = i - j; break;
}**
**`assume: f:$s0, g: $s1, h:$s2, i:$s3, j:$s4, k:$s5`**
---
<p style="font-size:20px;color:#b30000;font-weight: bold;">
Example: loop
</p>

MIPS assembly code:
assume : sum in $s1 , i in $S0

---
# Assignment Statements:
<p style="font-size:20px;color:#003300;font-weight: bold;">
Ex1:
</p>

<p style="font-size:20px;color:#003300;font-weight: bold;">
Ex2:
</p>
