28
Assembly Language - Lab (2) 1

[ASM] Lab2

Embed Size (px)

Citation preview

Page 1: [ASM] Lab2

1

Assembly Language - Lab (2)

Page 2: [ASM] Lab2

AgendaWhat is Assembly Language?Computer OrganizationMain memoryMemory Modes:

Real Mode (8086) Protected Mode (80386)

Page 3: [ASM] Lab2

What is Assembly Language? Assembly Language is a low-level (machine‐level) programming

language that uses mnemonics instead of numeric codes (0’s, 1’s) to simplify programming.

For example, the instruction in machine language which means copy the content from AX register to BX register is: 8B D8

While same instruction written in assembly is: mov BX, AX

Each statement in assembly code has a one-to-one relationship with machine language instructions, in other words each statement corresponds to a single machine code instruction.

3

Page 4: [ASM] Lab2

To run a program written in assembly language, we should have a converter (or translator) which converts these labels and mnemonics to their corresponding machine codes in 0’s and 1’s. This converter is called an assembler.

4

Machine CodeAssemblerAssembly Code

To run a program written in a high level language we should

have a converter called …..?

What is Assembly Language?

Page 5: [ASM] Lab2

Computer Organization

Main Memory

CPU

R2 R3R1

ALUControl unit

R4

Page 6: [ASM] Lab2

Main Memory It is the place to store data (and instructions) temporarily. Each location (byte) in memory has content (value) and a unique

label (address). Often, memory is used in larger chunks than single bytes. As shown

below:

Main Memory

Byte

Page 7: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

CPU 8086

Register Size 16 bits

Main Memory 1 MByte

Program Segment 64 k

(2¿¿20𝑏𝑦𝑡𝑒)¿

Page 8: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

4 General Purpose AX : Accumulator BX : Base CX: Count DX: Data

4 Index and Pointers DI: Destination Index SI : Source Index BP: Base Pointer SP : Stack pointer

4 Segments registers DS: Data Segment. CS: Code Segment SS: Stack Segment ES: Extra Segment

Instruction Pointers IP: Instruction Pointer

Flags ZF , SF, OF, CF...

Page 9: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

4 General PurposeEach of these registers could be decomposed into two 8‐bit registers.

AH and AL are dependent on AX. Changing AX’s value will change AH and AL values and vice versa.

Page 10: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

Index and Pointers (SI, DI)They are often used as pointers to memory items, but can be used for other purposes as the general‐purpose registers. They cannot be decomposed into 8‐bit registers.

Page 11: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

Index and Pointers (BP, SP)They are used to point to data in the stack and are called the Base Pointer and Stack Pointer, respectively.

StackBP

SP

Page 12: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

4 Segments registers (CS, DS, SS, ES)They keep the starting address of memory chunk used for different parts of a program.

CS stands for Code Segment, DS for Data Segment, SS for Stack Segment, and ES for Extra Segment. ES is used as a temporary segment register.

Page 13: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

Instruction Pointers (IP)This register is used with the CS register to keep track of the address of the next instruction to be executed by the CPU.

Flags (ZF , SF, OF, CF...)The FLAGS register stores important information about the results of the last executed operation. This information is stored as individual bits in this register. For example, there is a specific bit called the Zero flag (Z flag). This Z flag is 1 if the result of the last operation was zero otherwise Z flag is set to zero. Not all instructions modify the bits in FLAGS.

Page 14: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

As shown before, memory in real mode (8086) is limited to only one megabyte (220 bytes).

Valid address range is from (in hex) 00000 to FFFFF. These addresses require a 20‐bit number.

Will it fit in the segment registers?

Page 15: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

A program is often divided into 3 segments, which are Code, Data, and Stack segments where its starting address is stored in segment registers. And each of these segments must begins on a paragraph boundary (i.e. its address is divisible by 16). Therefore, the starting address of any segment always begins with four 0‐bits.

In addition, an offset register is used to address memory locations within each segment. The size of each segment is 64KBat most.

Why?

Page 16: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

To find the physical address (20‐bit) from segment‐offset pair, use the following relation:

16 * segment register + offset register

Multiplying by 16 is equivalent to left shifting the binary value 4 times. This done to return the four 0 bits which not stored physically in the segment register.

Why do we multiply by

16?

Page 17: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

Examples:1. Segment Register: 047C

Offset Register: 0048Physical address: 047C0 + 0048 = 04808

2. Segment Register: 047DOffset Register: 0038Physical address: 047D0 + 0038 = 04808

And we can also get the same address when we add 047E0 with 0028.

Same address!

Page 18: [ASM] Lab2

Memory Modes:Real Mode (8086) – 16 Bit Registers

Disadvantages:1. A single segment can only contain 64K of memory (the upper

limit of the 16‐bit offset register). 2. Each byte in memory does not have a unique segmented

address.

Page 19: [ASM] Lab2

Memory Modes:Protected Mode (80386) – 32 Bit Registers

CPU 80386

Register Size 32 bits

Main Memory 4 Gbyte

Program Segment 4 Gbyte

(2¿¿32𝑏𝑦𝑡𝑒)¿

Page 20: [ASM] Lab2

Memory Modes:Protected Mode (80386) – 32 Bit Registers

In 386 CPU, registers become 32‐bit wide except segment registers (selectors) remain 16‐bits as they are.

Two new 16‐bit segment registers are also added, FS and GS. This extension made the single segment size up to 4GB.

Page 21: [ASM] Lab2

Memory Modes:Protected Mode (80386) – 32 Bit Registers

So how does a segment register store the 32‐bit address of memory? Segment register is interpreted differently in protected mode. It is

interpreted as an index into a descriptor table than a register stores the starting address of the segment.

Descriptor table: is a table containing physical addresses of all segments beside some other information about these segments. It is stored in memory and its location is stored in a special register. (local or global)

Page 22: [ASM] Lab2

Memory Modes:Protected Mode (80386) – 32 Bit Registers

.

.

.

16 bits CS Register

32 bits Address of the CS in Memory

Main Memory

Descriptor Table

Page 23: [ASM] Lab2

Memory Modes:Protected Mode (80386) – 32 Bit Registers

As explained before, this mode allows the program segment to be of size 4GB, so only one segment of only one program can take the whole memory size to its own?

Page 24: [ASM] Lab2

Memory Modes:Protected Mode (80386) – 32 Bit Registers

For that problem protected mode uses a technique called virtual memory.

The basic idea of virtual memory system is to only keep the data and code in physical memory that programs are currently using.

Other data and code are stored temporarily on disk until they are needed again.

Page 25: [ASM] Lab2

Memory Modes:Protected Mode (80386) – 32 Bit Registers

Furthermore, segments can be divided into smaller 4K‐sized units called pages, so each segment can be divided to 220 pages.

The virtual memory system works with pages now instead of segments.

Page 26: [ASM] Lab2
Page 27: [ASM] Lab2

27

Questions?

Page 28: [ASM] Lab2

28

Thank you