COAL Language Helping (Short Notes)
COAL Language Helping (Short Notes)
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
- 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
- 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
25
Assembly language constructs
Variable Declaration
C++ ASM
26
Assembly Language -
Constructs (Arithmetic
Operations)
27
Assembly language constructs
Arithmetic Operations
29
Assembly Language -
Constructs (BITWISE
Operations)
30
Assembly language constructs
Bitwise Operations
31
Assembly language constructs
Bitwise Operations
<< 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
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
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
37
Assembly language constructs
Conditional statements
- If-else statement
39
Assembly language constructs
Conditional statements
- 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: 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
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?
54
Thoughts about
55
Thoughts about
Course
56
Thoughts about
Self improvement
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