0% found this document useful (0 votes)
12 views

COAL Language Helping (Short Notes)

The document discusses the importance of learning assembly language, highlighting its real-world relevance and its role in understanding computer architecture. It covers the basics of assembly language, including constructs, variable declarations, arithmetic and bitwise operations, conditional statements, and loop structures. Additionally, it includes thoughts on course improvement and self-reflection for personal growth.

Uploaded by

f223192
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

COAL Language Helping (Short Notes)

The document discusses the importance of learning assembly language, highlighting its real-world relevance and its role in understanding computer architecture. It covers the basics of assembly language, including constructs, variable declarations, arithmetic and bitwise operations, conditional statements, and loop structures. Additionally, it includes thoughts on course improvement and self-reflection for personal growth.

Uploaded by

f223192
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Computer Organization and

Assembly Language
Muhammad Talha Arif
Table of content
- Motivation
- Discussion about Assembly language
- Basics of assembly language
- Relating high-level programming language features in low-level
programming language
- Discussion about Project
- Thoughts about:-
- Course
- Self improvement. How can I improve myself?
- QA’s

2
Motivation

3
Motivation
- Why Learn Assembly Language?
- Real-World Relevance
- Understanding the Computer
- Improving Problem-Solving
- Applied in your "Pong" game project
- HEC Core subject

4
Motivation
- Can I pass this course?
- Yes, you can still pass this course

5
Assembly Language - BASIcs

6
Discussion about assembly language
- What is assembly language?
- A low-level programming language that is translated into machine code
(binary instructions) the CPU can execute.
- Difference from High-Level Languages:
- No abstraction, requires direct manipulation of memory, registers,
and hardware.
- Why it’s useful:
- It's used to control hardware directly, giving programmers fine
control over resources like memory, registers, and CPU instructions.

7
Basics Constructs in assembly language
- Registers:
- The basic storage locations within the CPU used to hold data for
operations. For example, AX, BX, CX, and DX.
- Memory:
- Assembly language works closely with memory—how to define and
manipulate variables and access data.
- Instructions:
- Instructions such as MOV, ADD, SUB and JMP etc
- Interrupts and I/O:
- Using interrupts (e.g., BIOS interrupt 0x16 for keyboard input) to
interact with the system.

8
Basics Constructs in assembly language

Fig. Simple Computer Model [1] 9


Basics Constructs in assembly language

Fig. Central Processing Unit [1] 10


Basics Constructs in assembly language
General Purpose Registers

Register Name Usage

AX Accumulator - Holds Data


Register - Used in arithmetic operations, logic
operations and transfer instructions

BX Base register - Holds the address


- Can be used for indexing

CX Counter - Used in loops as counter


register - Shifting and rotating bits

DX Data Register - Used for I/O operations


- Used in arithmetic operations. For example
stores remainder in division operation and
high part of the result in multiplication
11
operation (especially for 32-bit products)
Basics Constructs in assembly language
General Purpose Registers

Register Name Usage

SI Source Index - Store an offset address for src operand


Register - Works with data segment (DS)

DI Destination - Store an offset address for dest operand


Index Register - Works with extra segment (ES)

BP Base Pointer - Access the parameters passed to a function


Register or a subroutine

SP Stack Pointer - Points to maximum memory offset (0xFFFE)


Register - Works with stack segment (SS)

IP Instruction - Points to next executing instruction


Pointer - Works with code segment (CS)
12
Register
Basics Constructs in assembly language
Segment Registers

Register Name Usage

CS Code Segment - Holds the base/starting address of all


executable instructions

DS Data Segment - Default base location of memory variables


- CPU calculate the offset address of these
variables

ES Extra Segment - Default base location of extra memory


variables
- Used for string manipulations

SS Stack Segment - Holds the base location of current program


stack
- LIFO Data structure. Last inserted element
13
will delete first. PUSH and POP instruction
Basics Constructs in assembly language
Flag Register

- Flag register contain 16 bits. Shows status of activities


- Arithmetic and logic instructions changes the flag status
- A single bit instruction whose meaning is independent of
from any other bit

Fig. Flag Register [2] 14


Basics Constructs in assembly language
Kinds of Flag

- Status Flag:
- Arithmetic and logic instructions will change the flag status
- There are 6 status flags in 16 bit register
- Control Flag:
- Control the operations of CPU
- There are 3 control flags in 16 bit register

15
Basics Constructs in assembly language
Status Flag

- Carry flag:
- 1 when arithmetic operation generate carry of borrow out of the MSB
otherwise 0
- Zero flag:
- 1 when reminder of an arithmetic operation is 0 as result otherwise 0
- Used to control the control of structure programs. Like jump zero
(jz) and jump not zero (jnz)
- Sign flag:
- 1 when reminder of an arithmetic operation is negative otherwise 0

16
Basics Constructs in assembly language
Status Flag

- Overflow flag:
- 1 when the reminder of an arithmetic operation is big to fit in the
destination otherwise
- Parity flag (Check bit):
- 1 when there is even number of 1’s bits in the result otherwise 0
- Used for error detection
- Auxiliary flag:
- 1 when an arithmetic operation generate carry to another nibble (4
bits) otherwise 0

17
Basics Constructs in assembly language
Control flag

- Interrupt flag:
- 1 when microprocessor will recognize/receive interrupt requests from
the peripherals (I/O) devices otherwise 0
- Direction flag:
- STD: set direction = 1 (backward processing), then index register
will move from end to beginning (auto decrement)
- CLD: set direction = 0 (forward processing), then index register will
move from beginning to end (auto increment)
- Trap flag:
- Used when debugging is required
- Set TF = 1, when single step mode (debugging) is needed
- Set TF = 0, when single step mode (debugging) is not needed 18
Basics Constructs in assembly language
ASCII Code

- ASCII stands for American Standard Code for Information


Interchange
- It is character encoding standard for electronic
communication
- It can be 7 bit character code (range 0-127) or 8 bit
character code (range 0-255)
- First 31 (range 0-31) are reserved to control peripherals
- Remaining range 32-127 is assign to character like
alphabetic, digits and symbols
19
Basics Constructs in assembly language

Fig. ASCII Table [3] 20


Basics Constructs in assembly language

Fig. Fetch execute lifecycle [4] 21


Assembly Language -
Constructs
22
Assembly language constructs
Like high-level programming languages, low-level programming
languages provides some constructs such as:-

- Variable Declaration
- Arithmetic Operations
- Bitwise Operations
- Conditional statements and Jumps
- Loop structures
- Functions

23
Assembly Language -
Constructs (Variable
Declaration)
24
Assembly language constructs
Variable Declaration

Datatype Name Size

DB Define Byte 1 Byte = 8 bits

DW Define Word 2 Byte = 16 bits

DD Define Double 4 Byte = 32 bits


Word

DQ Define Quad 8 Byte = 64 bits


Word

25
Assembly language constructs
Variable Declaration

C++ ASM

26
Assembly Language -
Constructs (Arithmetic
Operations)
27
Assembly language constructs
Arithmetic Operations

Symbol Operation Name Code in ASM Description

/ Division DIV OPERAND - AX is divided by an 8-bit


operand. The quotient is stored
in AL, and the remainder is
stored in AH.
- DX (32-bit dividend) is divided
by a 16-bit operand. The
quotient is stored in AX, and
the remainder is stored in DX.

* Multiplication MUL OPERAND - AX is multiplied by the operand


(8-bit or 16-bit). - The result
is a 16-bit or 32-bit product
stored in AX (low part) and DX
(high part).
28
Assembly language constructs
Arithmetic Operations

Symbol Operation Name Code in ASM Description

+ Addition ADD DEST, - Adds SRC to DEST and stores the


SRC result in DEST.
- Example: ADD AX, BX adds BX to
AX and stores the result in AX.

- Subtraction SUB DEST, - Subtracts SRC from DEST and


SRC stores the result in DEST.
- Example: SUB AX, BX subtracts BX
from AX and stores the result in
AX.

29
Assembly Language -
Constructs (BITWISE
Operations)
30
Assembly language constructs
Bitwise Operations

Symbol Operation Name Code in ASM Description

& Bitwise And AND DEST, - Each bit of the result is 1 if


SRC both corresponding bits of the
operands are 1, otherwise 0.

| Bitwise OR OR DEST, - Each bit of the result is 1 If


SRC either of the bits is 1,
otherwise 0.

31
Assembly language constructs
Bitwise Operations

Symbol Operation Name Code in ASM Description

^ Bitwise XOR XOR DEST, Each bit of the result is 1 if the


SRC corresponding bits of the operands
are different, otherwise 0.

~ Bitwise NOT NOT DEST Each bit of DEST is flipped (i.e., 0


becomes 1 and 1 becomes 0).

<< Bitwise Shift SHL DEST, Each shift left discards the leftmost
Left COUNT bit and fills the rightmost position
with 0.

>> Bitwise Shift SHR DEST, Each shift right discards the
Right COUNT rightmost bit and fills the leftmost 32
position with 0.
Assembly language constructs
Rotation Operations

Symbol Operation Name Code in ASM Description

ROL Rotate Left ROL DEST, Rotates the bits of DEST to the left
COUNT by the number of positions specified
in COUNT.

ROR Rotate Right ROR DEST, The bits shifted out of the right end
COUNT are placed at the left end.

33
Assembly language constructs
Rotation Operations

Symbol Operation Name Code in ASM Description

RCL Rotate through RCL DEST, Rotates the bits of DEST to the left
Carry Left COUNT by the number of positions specified
in COUNT, with the carry flag as an
extra bit that is shifted into the
operand.

RCR Rotate through RCR DEST, Rotates the bits of DEST to the right
Carry Right COUNT by the number of positions specified
in COUNT, with the carry flag as an
extra bit that is shifted into the
operand.

34
Assembly Language -
Constructs (CONDITIONAL
STATEMENTS AND JUMPS)
35
Assembly language constructs
Conditional statements

- JE (Jump if Equal): Jumps if LHS == RHS (i.e., when the Zero Flag is set).
- JC (Jump if Carry): Jumps if there is a carry (i.e., LHS < RHS for unsigned
numbers).
- JAE (Jump if Above or Equal): Jumps if LHS >= RHS for unsigned numbers (i.e., if
there’s no carry).
- JG (Jump if Greater): Jumps if LHS > RHS for signed numbers (i.e., if Zero Flag
is clear and the Carry Flag is clear).
- JGE (Jump if Greater or Equal): Jumps if LHS >= RHS for signed numbers (i.e., if
the result is non-negative).
- JL (Jump if Less): Jumps if LHS < RHS for signed numbers (i.e., if the Sign Flag
is set).
- JB (Jump if Below): Jumps if LHS < RHS for unsigned numbers (i.e., if the Carry
Flag is set).
- JA (Jump if Above): Jumps if LHS > RHS for unsigned numbers (i.e., if the Carry
Flag is cleared).

36
Assembly language constructs
Conditional statements

- JO (Jump if Overflow): Jumps if the result of the subtraction caused an overflow


(signed comparison).
- JNO (Jump if No Overflow): Jumps if no overflow occurred (i.e., the subtraction
result did not overflow).
- JS (Jump if Signed): Jumps if the result of the subtraction is negative (signed
comparison).
- JNS (Jump if Not Signed): Jumps if the result of the subtraction is non-negative
(signed comparison).
- JNE (Jump if Not Equal): Jumps if LHS ≠ RHS (i.e., when the Zero Flag is clear).
- JNAE (Jump if Not Above or Equal): Jumps if LHS < RHS for unsigned numbers
(i.e., when there’s a carry).
- JAE (Jump if Above or Equal): Jumps if LHS >= RHS for unsigned numbers (i.e., if
the carry flag is cleared).

37
Assembly language constructs
Conditional statements

- If-else statement

Fig. Decision Making [5] 38


Assembly language constructs
Conditional statements

39
Assembly language constructs
Conditional statements

- Nested if-else statement

Fig. Decision Making [5] 40


Assembly Language -
Constructs (loop structures)
41
Assembly language constructs
Loop Structures

- For-loop

42
Assembly language constructs
Loop Structures

- While-loop

43
Assembly language constructs
Loop Structures

- Do-while-loop

44
Assembly language constructs
Translating a c code to asm

Hint: Changing the logical


operator from || (OR) to &&
(AND) or vice versa can alter
the behavior of a condition
when multiple variables are
involved. 45
Assembly language constructs
Translating a c code to asm

Hint: Why we use || here because c can never be q and Q at the same time. It is similar to
46
a situation where you can’t be at university and at home at the same time
Assembly Language -
Constructs (Functions)
47
Assembly language constructs
Functions C++

ASM

48
Assembly language constructs
Stack Segment

- Used for managing function calls, local


variables, return addresses, and saved
registers during program execution.
- Region of memory used for temporary storage
and data management in a Last-In-First-Out
(LIFO) manner.

49
Assembly Language - project

50
project
Blinking Foreground (7th Bit)

51
project
Foreground Color (3-6th Bit)

52
project
Background Color (0-2th Bit)

53
project
How to use these colors?

- Let’s say I want my foreground to blink. Foreground color


should be Red and background should be Cyan. I can use
bitwise operators.
(BLINK_FOREGROUND_ENABLE | FOREGROUND_COLOR_RED | BACKGROUND_COLOR_CYAN)

- This gives us:


- (10000000b | 00000100b | 00110000b) = 0xB4

54
Thoughts about

55
Thoughts about
Course

- How can I improve this course from your perspective?


- What do you think should be improved in this course?
- How you feel about the execution of this course?
- What makes you think that this course is dry?

56
Thoughts about
Self improvement

- How can I improve myself?


- How was my behavior with you till now?
- Where do you think that I am lacking and I should need to
improve it?
- Behavior
- Lab Execution
- Friendly Environment
- Flexible Deadline
- Labs based on what?

57
Question and answer

58
References
[1] https://yassinebridi.github.io/asm-docs/asm_tutorial_01.html

[2] https://mrcet.com/downloads/digital_notes/EEE/14062023/Microprocessors%20&%20Microcontrollers.pdf

[3] https://simple.m.wikipedia.org/wiki/File:ASCII-Table-wide.svg

[4] https://learnlearn.uk/alevelcs/fetch-execute-cycle/

[5] https://www.geeksforgeeks.org/decision-making-c-cpp/

59

You might also like