IKI10230 Pengantar Organisasi Komputer Kuliah No. 04: Assembly Language
IKI10230 Pengantar Organisasi Komputer Kuliah No. 04: Assembly Language
IKI10230 Pengantar Organisasi Komputer Kuliah No. 04: Assembly Language
3 Maret 2004
L. Yohanes Stefanus ([email protected])
Bobby Nazief ([email protected])
1
Revisi Jadwal
Kuliah
TGL NO TOPIK PC Hm
11-Feb 1 Pendahuluan, Organisasi Komputer 1
18-Feb 2 Stored Program Computers 2.1, 2.2, 2.3, 2.4
25-Feb 3 Tools, Sistem Bilangan, Operasi +, - 1.1 6.1
03-Mar 4 Assembly Language 1.3
10-Mar 4,5 Assembly Language, Data Transfer Operations
17-Mar 6,7 Arithmetic & Logical Operations 2.1,3.1, 3.2
24-Mar 8,9,10 Control Structures, Array/String & FP Operations 2.2,5,6
31-Mar 11,12 The CALL and RET Instructions, Multi-module 4.3, 4.4,4.6
07-Apr 13 Interfacing Assembly with HLL, Review 4.7
14-Apr UTS
21-Apr 14 Compile-Assembly-Link-Load 1.4
28-Apr 15,16 Micro Architecture & Control Unit 7.1-7.5
05-Mei 17,18 Memori, Virtual Memory 5.1, 5.4, 5.5, 5.7
12-Mei 19,20 I/O: Polling & Interrupt, Exceptions 4.1, 4.2
19-Mei 21 Operasi Aritmatika: Mul & Div 6.3,6.6,6.7
26-Mei Review
2
REVIEW
3
Review: Pengelompokkan
Bit
° Bit String:
INTEL
• 4 bit nibble
• 8 bit byte
• 16 bit word
• 32 bit double-word
• 64 bit quad-word
Alamat
int i = 90; (32 bit)
90 = 0x5A = i 00000000 0101 1010
00000001 0000 0000
0000 0000 0000 0000 0000 0000 0101 1010 00000002 0000 0000
00000003 0000 0000
j 00000004 0011 0100
00000005 0001 0010
00000006 0000 1111
00000007 0000 0000
int j = 987700;
987700 = 0x000F1234 =
0000 0000 0000 1111 0001 0010 0011 0100
FFFFFFFF
5
Review: Two’s Complement
Numbers
0000 ... 0000 0000 0000 0000two = 0ten
0000 ... 0000 0000 0000 0001two = 1ten
0000 ... 0000 0000 0000 0010two = 2ten
...
0111 ... 1111 1111 1111 1101two = 2,147,483,645ten
0111 ... 1111 1111 1111 1110two = 2,147,483,646ten
0111 ... 1111 1111 1111 1111two = 2,147,483,647ten
1000 ... 0000 0000 0000 0000two = –2,147,483,648ten
1000 ... 0000 0000 0000 0001two = –2,147,483,647ten
1000 ... 0000 0000 0000 0010two = –2,147,483,646ten
...
1111 ... 1111 1111 1111 1101two = –3ten
1111 ... 1111 1111 1111 1110two = –2ten
1111 ... 1111 1111 1111 1111two = –1ten
° One zero, 1st bit is called sign bit
• but one negative with no positive –2,147,483,648ten
6
Review: Sign
extension
° Convert 2’s complement number using n bits to
more than n bits
° Simply replicate the most significant bit (sign bit)
of smaller to fill new bits
•2’s comp. positive number has infinite 0s
•2’s comp. negative number has infinite 1s
•Bit representation hides leading bits;
sign extension restores some of them
•16-bit -4ten to 32-bit:
7
ARSITEKTUR INTEL X86:
DARI PANDANGAN PEMROGRAM
8
Register: most frequently accessed operand
Computer
Processor Memory Devices
Control Input
Store
Datapath
Registers Output
Load
9
Sumber Daya Komputasi: Register & Memori
(64G)
10
Sumber Daya Komputasi: Set
Instruksi
Data Transfers memory-to-memory move
register-to-register move
memory-to-register move
11
ORGANISASI MEMORI
12
Flat Memory
Model
° The real-address mode model uses the memory model for the
Intel 8086 processor, the first IA processor (for backward
compatibility).
° The real-address mode uses a specific implementation of
segmented memory in which the linear address space for the
program and the operating system/executive consists of an array of
segments of up to 64 Kbytes in size each.
° The maximum size of the linear address space in real-address
mode is 220 bytes.
15
REGISTERS
16
x86 Registers
18
Status Register: EFLAGS
19
Status
Flags
° SF (bit 7) Sign flag
• Set equal to the most-significant bit of the result, which is the sign bit
of a signed integer. (0 indicates a positive value and 1 indicates a
negative value.)
° ZF (bit 6) Zero flag
• Set if the result is zero; cleared otherwise.
° CF (bit 0) Carry flag
• Set if an arithmetic operation generates a carry or a borrow out of the
most-significant bit of the result; cleared otherwise.
° OF (bit 11) Overflow flag
• Set if the integer result is too large a positive number or too small a
negative number (excluding the sign-bit) to fit in the destination
operand; cleared otherwise.
° PF (bit 2) Parity flag
• Set if the least-significant byte of the result contains an even number
of 1 bits; cleared otherwise.
° AF (bit 4) Adjust flag
• Set if an arithmetic operation generates a carry or a borrow out of bit 3
of the result; cleared otherwise. Used in BCD arithmetic.
20
System Flags
21
Segment Registers
• CS code segment
- where the instructions being executed are stored.
• DS data segment
• ES,FS,GS extra (data) segment
with possible configuration:
- one for the data structures of the current module,
- another for the data exported from a higher-level module,
- a third for a dynamically created data structure,
- a fourth for data shared with another program.
• SS stack segment
22
Use of Segment Registers in Flat Memory
Model
° The segment registers are loaded with segment selectors
that point to overlapping segments, each of which begins
at address 0 of the linear address space.
° Typically, two overlapping segments are defined: one for
code (pointed to by CS) and another for data and stacks.
23
Use of Segment Registers in Segmented Memory
Model
° Each segment register is ordinarily loaded with a
different segment selector so that each segment
register points to a different segment (up to 6
segments) within the linear address space.
24
Data Storage
25
Storage of Fundamental Data
Type
26
Storage of Numeric Data Type
27
PROCESSOR OPERATION MODE
28
3 Modes of
Operation
° Protected mode
• the native state of the processor
• all instructions and architectural features are available, providing the highest
performance and capability
• recommended mode for all new applications and operating systems
• the processor can use any of the memory models
• ability to directly execute “real-address mode” 8086 software in a protected,
multitasking environment: virtual-8086 mode
° Real-address mode
• provides the programming environment of the Intel 8086 processor with a few
extensions
• the processor is placed in real-address mode following power-up or a reset
• only supports the real-address mode memory model
° System management mode
• provides an operating system with a transparent mechanism for implementing
platform-specific functions such as power management and system security
• the processor enters SMM when the external SMM interrupt pin (SMI#) is
activated or an SMI is received from the advanced programmable interrupt
controller (APIC)
• in SMM, the processor switches to a separate address space while saving the
entire context of the currently running program or task
29
Protected Mode
° a (segment) selector value is an index into a descriptor table.
° the segments are not at fixed positions in physical memory. In fact,
they do not have to be in memory at all!
° Protected mode uses a technique called virtual memory. The
basic idea of a virtual memory system is to only keep the data and
code in memory that programs are currently using.
° 16-bit:
• offsets are still 16-bit quantities. As a consequence of this, segment sizes are
still limited to at most 64K.
° 32-bit:
• offsets are expanded to be 32-bits. This allows an oset to range up to 4 billion.
Thus, segments can have sizes up to 4 gigabytes.
• segments can be divided into smaller 4K-sized units called pages.
° In Windows 3.x:
• standard mode referred to 286 16-bit protected mode
• enhanced mode referred to 32-bit mode.
31
Contoh program dalam real-
mode
° hello_m.asm:
1. segment .text
2. ..start:
3. mov ax,DATA
4. mov ds,ax
5. mov dx,hello
6. mov ah,9
7. int 0x21
8. ....
9. segment DATA
10. hello: db 'hello, world', 13, 10, '$‘
° debug hello_m.exe:
AX=0B3D BX=FFFF CX=FE5A DX=0000 SP=010A BP=0000 SI=0000 DI=0000
DS=0B3C ES=0B2B SS=0B3D CS=0B3B IP=000D NV UP EI PL NZ NA PO NC
0B3B:000D BA0B00 MOV DX,000B
-d ds:b
0B3C:0000 68 65 6C 6C 6F hello
0B3C:0010 2C 20 77 6F 72 6C 64 0D-0A 24 00 00 00 00 00 00 , world..$......
32
Intel x86 Assembly Program
33
NASM Assembly-Program Format
[label:] instruction operands ; comment
° label: optional
• represents the address of memory location storing the instruction
• to be used as reference for:
1. data access
2. jump-address
° instruction:
• data transfer
• arithmetic & logic
• program sequencing & control
• i/o
• ...
° operands:
• register
• memory
• immediate
• implied
° comment
• no comment
34
LABEL
35
Review: The Program
is ...
° lokasi instruksi
0000 0846 Add 8,4,6 ; 8 [4] + [6]
; [8] = 61 + 17 = 78
0002 1686 Sub 6,8,6 ; 6 [8] – [6]
; [6] = 78 – 17 = 61
36
Label
° Label:
• Valid characters in labels are:
- letters, numbers, _, $, #, @, ~, ., and ?
• The only characters which may be used as the first character of
an identifier are:
- letters, . (period), _, ?
- A label beginning with a single period is treated as a local
label, which means that it is associated with the previous
non-local label. So, for example:
label1 ; some code
.loop ; some more code
jne .loop
ret
label2 ; some code
.loop ; some more code
jne .loop
ret 37
Contoh: label dalam
tugas0a.asm*
1. segment .data
2. data1db 11h
3. data2dw 2222h
4. data3dd 33333333h
5. datatmp times 9 db 0ffh
1. segment .bss
2. stacks resd 1
1. segment .text
2. global _asm_main
3. _asm_main:
4. mov eax,10 ; decimal number, value = 10
5. mov edx,eax ; register-to-register transfer
6. mov esi,data1 ; esi points to data1
39
Review: Bahasa Mesin Bahasa Rakitan
40
Register Transfer Notation
° Notasi yang menggambarkan proses pertukaran data yang
terjadi pada eksekusi instruksi:
• arah: dari sumber ke tujuan
• operasi: ‘+’, ‘-’, …
° Sumber/Tujuan Data:
• Register
• Memori
• I/O Device
° Contoh:
• Pertukaran data:
Move R1,LOC R1 [LOC] ; isi lokasi memori ‘Loc’ di- ;
copy-kan ke register R1
• Operasi:
Add R3,R1,R2 R3 [R1] + [R2] ; isi register R1 dijumlahkan ;
dengan isi register R2, ; hasilnya disimpan di ; register R3
41
Review: Jumlah Operand Kelas Set
Instruksi
° 3-address instruction
Add C,A,B ; C A] + [B]
Operation Destination,Source1,Source2
atau
Operation Source1,Source2,Destination
° 2-address instruction
Format Instruksi
Add A,B ; A A] + [B]
Operation Destination,Source Intel x86
° 1-address instruction
Load B ; acc B
Add A ; acc acc] + [A]
° 0-address instruction
Push B ; tos B
Push A ; tos A; [next] = B
Add ; tos tos] + [next]
42
Instruction
Format
44
Operand Addressing
° Register
• refers to the data (content) of a register
mov eax,ebx EAX EBX
89 d8 100 100
° Immediate
• refers to a fixed value that is hard-coded into the instruction itself
mov eax,0x10
EAX
b8 10 00 00 00
0x00000010 0xb8 0x00000010
° Memory
• refers to the data (content) of a memory location
mov eax,[data] ; eax [data] (direct memory access)
a1 d0 92 00 00 ; data is located at 0x000092d0
mov eax,[ebx] ; eax [[ebx]] (indirect memory access)
8b 03 ; data location = [ebx]
45
(Direct) Memory
Operand
DATA DD 0x0000FFFF
...
MOV EAX,[DATA]
DATA =
0x000092D0 0x0000FFFF
EAX 0x0000FFFF
46
(Indirect) Memory
Operand
DATA DD 0x0000FFFF
...
MOV EBX,DATA
MOV EAX,[EBX]
0x00009200
EAX 0x0000FFFF
47
Register Operands
° Source and destination operands can be any of:
• 32-bit GP registers: EAX, EBX, ECX, EDX, ESI, EDI, ESP, EBP
• 16-bit GP registers: AX, BX, CX, DX, SI, DI, SP, BP
• 8-bit GP registers: AH, BH, CH, DH, AL, BL, CL, DL
• segment registers: CS, DS, SS, ES, FS, GS
• EFLAGS register
• system registers: GDTR (global descriptor table), IDTR (interrupt
descriptor table register)
48
Contoh: register-operand dalam
tugas0a.asm*
1. segment .data
2. data1db 11h
3. data2dw 2222h
4. data3dd 33333333h
5. datatmp times 9 db 0ffh
1. segment .bss
2. stacks resd 1
1. segment .text
2. global _asm_main
3. _asm_main:
4. mov eax,10 ; decimal number, value = 10
5. mov edx,eax ; register-to-register transfer
6. mov esi,data1 ; esi points to data1
° Character
• mov eax,'abcd' a b c d
1. segment .bss
2. stacks resd 1
1. segment .text
2. global _asm_main
3. _asm_main:
4. mov eax,10 ; decimal number, value = 10
5. mov edx,eax ; register-to-register transfer
6. mov esi,data1 ; esi points to data1
4
8
53
Contoh: memory-operand dalam tugas0a.asm*
1. segment .data
2. data1db 11h
3. data2dw 2222h
4. data3dd 33333333h
5. datatmp times 9 db 0ffh
1. segment .bss
2. stacks resd 1
1. segment .text
2. global _asm_main
3. _asm_main:
4. mov eax,10 ; decimal number, value = 10
5. mov edx,eax ; register-to-register transfer
6. mov esi,data1 ; esi points to data1
_p dd 0, 0, 1, 1
...
mov ebx,_p
mov esi,_i
...
add eax,[ebx+8*esi+0]
add edx,[ebx+8*esi+4]
...
55
DIRECTIVES
56
SECTION or SEGMENT
57
EXTERN & GLOBAL
1. segment .bss
2. stacks resd 1
1. segment .text
2. global _asm_main
3. _asm_main:
4. mov eax,10 ; decimal number, value = 10
5. mov edx,eax ; register-to-register transfer
6. mov esi,data1 ; esi points to data1
60
DB and friends: Declaring Initialised
Data
° DB, DW, DD, DQ and DT are used to declare
initialized data in the output file.
db 0x55 ; just the byte 0x55
db 0x55,0x56,0x57 ; three bytes in succession
db 'a',0x55 ; character constants are OK
db 'hello',13,10,'$' ; so are string constants
dw 0x1234 ; 0x34 0x12
dw 'a' ; 0x41 0x00 (it's just a number)
dw 'ab' ; 0x41 0x42 (character constant)
dw 'abc' ; 0x41 0x42 0x43 0x00 (string)
dd 0x12345678 ; 0x78 0x56 0x34 0x12
dd 1.234567e20 ; floating-point constant
dq 1.234567e20 ; double-precision float
dt 1.234567e20 ; extended-precision float
61
RESB and friends: Declaring Uninitialised Data
62
EQU: Defining Constants
header_len equ 16
...
mov ecx,header_len ; eax 16
63
TIMES: Repeating Instructions or Data
zerobuf: times 64 db 0
...
times 100 movsb
64
Contoh: pseudo-instructions dalam
tugas0a.asm* .data
1. segment
2. data1 db 11h
3. data2 dw 2222h
4. data3 dd 33333333h
5. datatmp times 9 db 0ffh
1. segment .bss
2. stacks resd 1
1. segment .text
2. global _asm_main
3. _asm_main:
4. mov eax,10 ; decimal number, value = 10
5. mov edx,eax ; register-to-register transfer
6. mov esi,data1 ; esi points to data1
66
Special Expressions
67
Operators
° |: Bitwise OR Operator
• bitwise OR
° ^: Bitwise XOR Operator
• bitwise XOR
° &: Bitwise AND Operator
• bitwise AND
° << and >>: Bit Shift Operators
• << gives a bit-shift to the left, >> gives a bit-shift to the right
• in NASM, such a shift is always unsigned
° + and -: Addition and Subtraction Operators
• do perfectly ordinary addition and subtraction
° *, /, //, % and %%: Multiplication and Division
• * is the multiplication operator
• / is unsigned division and // is signed division
• % and %% provide unsigned and signed modulo operators
° Unary Operators: +, -, ~ and SEG
• - negates its operand
• + does nothing (it's provided for symmetry with -)
• ~ computes the one's complement of its operand
• SEG provides the segment address of its operand
68
Contoh: expressions dalam tugas0a.asm*
1. segment .data
2. data1 db (1<<4)|1 ; [data1] = 11h
3. data2 dw 2222h
4. data3 dd 33333333h
5. datatmp times 9 db 0ffh
1. segment .bss
2. stacks resd 1
1. segment .text
2. global _asm_main
3. _asm_main:
4. mov eax,~0xEF&0xFF ; decimal number, value = 10
5. mov edx,eax ; register-to-register transfer
6. mov esi,data1 ; esi points to data1
70
hello.as
m
extern _printf
segment .data
the_str db "hello world", 10, 0
segment .text
global _asm_main
_asm_main:
enter 0,0
pusha
popa
mov eax,0 ; return back to main() – driver.c
leave
ret
71
EVALUASI TUGAS0
72
tugas0a.asm
1. segment .data
2. data1 db 11h
3. data2 dw 2222h
4. data3 dd 33333333h
5. data4 times 16 db 0