---
# System prepended metadata

title: 'Assignment Statements:'

---


<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 :
![](https://i.imgur.com/4hDy3W2.png)

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:
![](https://i.imgur.com/UFFYO8T.png)

e.g. :
![](https://i.imgur.com/1qCfvah.png)


---

<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.

![](https://i.imgur.com/Bs5a989.jpg)


---

<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. 

![](https://i.imgur.com/fCgzYR2.png)



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** .

![](https://i.imgur.com/5wQfOS1.png)

***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>

 ![](https://i.imgur.com/U6B6wF6.png)
 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>

![](https://i.imgur.com/Zq8bZc2.png)
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) .

![](https://i.imgur.com/xs9wLY8.png)

<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
  ![](https://i.imgur.com/LO0gy9u.png)


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

![](https://i.imgur.com/tJFvX8W.png)


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

![](https://i.imgur.com/5Rui8HT.png)


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

![](https://i.imgur.com/w2Zyn0N.png)

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

convert to assembly code:
![](https://i.imgur.com/fJYOXYa.png)


---

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

![](https://i.imgur.com/RmVqsnc.png)

 MIPS assembly code:
 assume : sum in $s1 , i in $S0
 ![](https://i.imgur.com/UB0msA1.png)


---

# Assignment Statements:

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

![](https://i.imgur.com/zxdvGNQ.png)

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


![](https://i.imgur.com/7eK5kxw.png)

