Transcript
Page 1: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

1

IKI10230Pengantar Organisasi Komputer

Kuliah no. 04: Assembly LanguageSumber:1. Paul Carter, PC Assembly Language2. Hamacher. Computer Organization, ed-53. Materi kuliah CS61C/2000 & CS152/1997, UCB4. Intel Architecture Software Developer’s Manual

3 Maret 2004

L. Yohanes Stefanus ([email protected])Bobby Nazief ([email protected])

bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/

Page 2: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

2

Revisi Jadwal Kuliah

TGL NO TOPIK PC Hm11-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,5Assembly Language, Data Transfer

Operations

17-Mar 6,7 Arithmetic & Logical Operations 2.1,3.1, 3.2

24-Mar 8,9,10Control Structures, Array/String & FP

Operations 2.2,5,6

31-Mar 11,12The 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

Page 3: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

3

REVIEW

Page 4: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

4

Review: Pengelompokkan Bit

° Bit String: INTEL

• 4 bit nibble

• 8 bit byte

• 16 bit word

• 32 bit double-word

• 64 bit quad-word

° Alamat lokasi memori• umumnya dinyatakan dengan bilangan heksa desimal

• contoh:

- lokasi memori 90 pada memori dengan ruang memori sebesar 64K (65536 = 216) dinyatakan dengan alamat:

0x005A

- jika ruang memori sebesar 232 (4G)

0x0000005A

Page 5: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

5

Review: Penyimpanan data multi-byte (Little Endian)

int j = 987700;

987700 = 0x000F1234 =

0000 0000 0000 1111 0001 0010 0011 0100

0000000000000001000000020000000300000004000000050000000600000007

FFFFFFFF

0101 1010

Alamat (32 bit)

0000 00000000 00000000 00000011 01000001 00100000 11110000 0000

int i = 90;

90 = 0x5A =

0000 0000 0000 0000 0000 0000 0101 1010

i

j

Page 6: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

6

Review: Two’s Complement Numbers

0000 ... 0000 0000 0000 0000two =

0ten0000 ... 0000 0000 0000 0001two =

1ten0000 ... 0000 0000 0000 0010two =

2ten. . .0111 ... 1111 1111 1111 1101two =

2,147,483,645ten0111 ... 1111 1111 1111 1110two =

2,147,483,646ten0111 ... 1111 1111 1111 1111two =

2,147,483,647ten1000 ... 0000 0000 0000 0000two =

–2,147,483,648ten1000 ... 0000 0000 0000 0001two =

–2,147,483,647ten1000 ... 0000 0000 0000 0010two =

–2,147,483,646ten. . . 1111 ... 1111 1111 1111 1101two =

–3ten1111 ... 1111 1111 1111 1110two =

–2ten1111 ... 1111 1111 1111 1111two =

–1ten

°One zero, 1st bit is called sign bit • but one negative with no positive –2,147,483,648ten

Page 7: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

7

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:

1111 1111 1111 1100two

1111 1111 1111 1111 1111 1111 1111 1100two

Page 8: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

8

ARSITEKTUR INTEL X86:

DARI PANDANGAN PEMROGRAM

Page 9: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

9

Register: most frequently accessed operand

Processor

Computer

Control

DatapathRegisters

Memory Devices

Input

OutputLoadLoad

StoreStore

Registers are in the datapath of the processor; if operands are in memory, we must transfer them to the processor to operate on them,And then transfer back to memory when done

Page 10: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

10

Sumber Daya Komputasi: Register & Memori

(64G)

Page 11: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

11

Sumber Daya Komputasi: Set InstruksiData Transfers memory-to-memory move

register-to-register movememory-to-register move

Arithmetic & Logic integer (binary + decimal) or FPAdd, Subtract, Multiply, Divide

not, and, or, set, clearshift left/right, rotate left/right

Program Sequencing & Control

unconditional, conditional Branchcall, returntrap, return

Synchronization test & set (atomic r-m-w)String search, translateGraphics (MMX) parallel subword ops (4 16bit add)

Input/Output Transfers register-to-i/o device move

Page 12: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

12

ORGANISASI MEMORI

Page 13: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

13

Flat Memory Model

° With the flat memory model, memory appears to a program as a single, continuous address space, called a linear address space.

° The linear address space is byte addressable, with addresses running contiguously from 0 to 236 - 1.

° An address for any byte in the linear address space is called a linear address.

Page 14: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

14

Segmented Memory Model

° With the segmented memory model, memory appears to a program as a group of independent address spaces called segments. When using this model, code, data, and stacks are typically contained in separate segments.

° To address a byte in a segment, a program must issue a logical address, which consists of a segment selector and an offset. The segment selector identifies the segment to be accessed and the offset identifies a byte in the address space of the segment.

° The programs running on an IA processor can address up to 16,383 segments of different sizes and types, and each segment can be as large as 236 bytes.

Page 15: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

15

Real-Address Mode 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.

Page 16: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

16

REGISTERS

Page 17: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

17

x86 Registers

Program Counter (PC)

Page 18: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

18

General Purpose Registers

° GP Registers have additional, specific functions:• EAX Accumulator for operands and results data.

• EBX Pointer to data in the DS segment.

• ECX Counter for string and loop operations.

• EDX I/O pointer.

• ESI Pointer to data in the segment pointed to by the DSregister; source pointer for string operations.

• EDI Pointer to data (or destination) in the segment pointed toby the ES register; destination pointer for string

operations.

• ESP Stack pointer (in the SS segment).

• EBP Pointer to data on the stack (in the SS segment).

Page 19: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

19

Status Register: EFLAGS

Page 20: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

20

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.

Page 21: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

21

System Flags

° IF (bit 9) Interrupt enable flag• Controls the response of the processor to maskable interrupt

requests. Set to respond to maskable interrupts; cleared to inhibit maskable interrupts.

° IOPL (bits 12, 13) I/O privilege level field• Indicates the I/O privilege level of the currently running program or

task. The current privilege level (CPL) of the currently running program or task must be less than or equal to the I/O privilege level to access the I/O address space. This field can only be modified by the POPF and IRET instructions when operating at a CPL of 0.

° NT (bit 14) Nested task flag• Controls the chaining of interrupted and called tasks. Set when the

current task is linked to the previously executed task; cleared when the current task is not linked to another task.

° VM (bit 17) Virtual-8086 mode flag• Set to enable virtual-8086 mode; clear to return to protected mode.

Page 22: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

22

Segment Registers

° Used to hold 16-bit segment selectors

• 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

Page 23: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

23

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.

Page 24: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

24

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.

Page 25: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

25

Data Storage

Page 26: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

26

Storage of Fundamental Data Type

Page 27: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

27

Storage of Numeric Data Type

Page 28: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

28

PROCESSOR OPERATION MODE

Page 29: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

29

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

Page 30: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

30

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.

° Windows 9X, Windows NT/2000/XP, OS/2 and Linux all run in paged 32-bit protected mode.

Page 31: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

31

Real Mode

° In real mode, memory is limited to only 1M (220) bytes. Valid address range from 0x00000 to 0xFFFFF.

° 20-bit address is constructed using 2 16-bit values:• The first 16-bit value is called the selector, stored in segment

register.

• The second 16-bit value is called the offset.

• The physical address referenced by a 32-bit selector:offset pair is computed by the formula:

16*selector + offset

- multiplying by 16 in hex is easy, just add a 0 to the right of the number

- for example, the physical addresses referenced by 047C:0048 is given by: 047C0 + 0048 = 04808

Page 32: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

32

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

Page 33: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

33

Intel x86 Assembly Program

Page 34: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

34

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 access2. jump-address

° instruction:• data transfer• arithmetic & logic• program sequencing & control• i/o• ...

° operands:• register• memory• immediate• implied

° comment• no comment

Page 35: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

35

LABEL

Page 36: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

36

Review: The Program is ...

° lokasi instruksi0000 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

can be represented by labels

Page 37: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

37

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

Page 38: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

38

Contoh: label dalam tugas0a.asm*

1. segment .data

2. data1 db 11h

3. data2 dw 2222h

4. data3 dd 33333333h

5. datatmp times 9 db 0ffh

6. segment .bss

7. stacks resd 1

8. segment .text

9. global _asm_main

10. _asm_main:

11. mov eax,10 ; decimal number, value = 10

12. mov edx,eax ; register-to-register transfer

13. mov esi,data1 ; esi points to data1

18. mov al,[esi] ; indirect memory access, load 1 byte

19. mov bx,[esi] ; indirect memory access, load 1 word

20. mov ecx,[esi] ; indirect memory access, load 1 double-word

21. mov edi,[data3] ; direct memory operand

Page 39: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

39

INSTRUCTIONS

Page 40: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

40

Review: Bahasa Mesin Bahasa Rakitan

0846: Add (8),(4),(6)

° Bahasa Mesin kumpulan bit yang merepresentasikan Operasi & Operand

° Bahasa Rakitan representasi dari Bahasa Mesin dalam bahasa (kumpulan huruf & angka) yang lebih mudah dimengerti oleh manusia

mnemonic

8 [4] + [6]Bahasa Mesin Bahasa Rakitan

Register Transfer Notation

Page 41: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

41

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

° Nilai/content dari sumber data dinyatakan dengan• [sumber-data]

° 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

Page 42: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

42

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 instructionAdd A,B ; A A] + [B]Operation Destination,Source

° 1-address instructionLoad B ; acc B

Add A ; acc acc] + [A]

° 0-address instructionPush B ; tos BPush A ; tos A; [next] = B

Add ; tos tos] + [next]

Format Instruksi Intel x86

Page 43: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

43

Instruction Format

° Ukuran instruksi [n] bervariasi: 1 n 16 byte

0, 1, 2, 3, 4 1, 2 0,1 0,1 0, 1, 2, 3, 4 0, 1, 2, 3, 4

• Prefix: (Lock, Repeat), Overrides: Segment, Operand Size, Address Size

• ModR/M: Addressing Mode

• SIB: Scale, Index, Base

• Displacement: Displacement’s Value

• Immediate: Immediate’s Value

° Konvensi: OPcode dst,src ; dst [dst] OP [src]

° Contoh:MOV EAX,EBX ; register

MOV EAX,[DATA] ; displacement

MOV EAX,0x10 ; immediate

REP MOV EDX,EAX ; prefix: REP

MOV EAX,[EBP+4*ESI+Offset] ; base+index*scale+displacement

...

Prefix Displacement ImmediateSIBOpcode ModR/M

Page 44: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

44

OPERANDS

Page 45: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

45

° Register• refers to the data (content) of a register

mov eax,ebx

89 d8

° Immediate• refers to a fixed value that is hard-coded into the instruction itself

mov eax,0x10

b8 10 00 00 00

° 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]

Operand Addressing

100

EBXEAX

100

EAX0xb8 0x000000100x00000010

Page 46: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

46

(Direct) Memory Operand

DATA DD 0x0000FFFF

...

MOV EAX,[DATA] ; EAX [DATA]

MOV EAX,[0x000090D0] ; EAX [0x000090D0]

DATA = 0x000092D0

MOV EAX,[DATA]

0x0000FFFF

EAX 0x0000FFFF

Page 47: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

47

(Indirect) Memory Operand

DATA DD 0x0000FFFF

...

MOV EBX,DATA ; EBX DATA=0x000092D0

MOV EAX,[EBX] ; EAX [[EBX]]

0x00009200

0x000092D0 0x0000FFFF

MOV EAX,[EBX]

EBX

EAX 0x0000FFFF

0x000092D0

MOV EBX,DATA

Page 48: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

48

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)

° Some instructions (DIV & MUL) use quadword operands contained in a pair of 32-bit registers.• EDX:EAX EDX: high-order dword, EAX: low-order dword

• Contoh:

mul ebx ; edx:eax [eax] * [ebx]

Page 49: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

49

Contoh: register-operand dalam tugas0a.asm*

1. segment .data

2. data1 db 11h

3. data2 dw 2222h

4. data3 dd 33333333h

5. datatmp times 9 db 0ffh

6. segment .bss

7. stacks resd 1

8. segment .text

9. global _asm_main

10. _asm_main:

11. mov eax,10 ; decimal number, value = 10

12. mov edx,eax ; register-to-register transfer

13. mov esi,data1 ; esi points to data1

18. mov al,[esi] ; indirect memory access, load 1 byte

19. mov bx,[esi] ; indirect memory access, load 1 word

20. mov ecx,[esi] ; indirect memory access, load 1 double-word

21. mov edi,[data3] ; direct memory operand

Page 50: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

50

Immediate Operands

° The maximum value allowed for an immediate operand varies among instructions, but can never be greater than the maximum value of an unsigned doubleword integer (232).

° Numeric• mov eax,100 ; decimal

• add eax,0a2h ; hex

• and eax,0xa2 ; hex again

• imul eax,ebx,242q ; octal

• push 01010011b ; binary

° Character• mov eax,'abcd'

° All arithmetic instructions (except DIV & IDIV instructions) allow the source operand to be an immediate value.

a b c d

Page 51: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

51

Contoh: immediate-operand dalam tugas0a.asm*

1. segment .data

2. data1 db 11h

3. data2 dw 2222h

4. data3 dd 33333333h

5. datatmp times 9 db 0ffh

6. segment .bss

7. stacks resd 1

8. segment .text

9. global _asm_main

10. _asm_main:

11. mov eax,10 ; decimal number, value = 10

12. mov edx,eax ; register-to-register transfer

13. mov esi,data1 ; esi points to data1

18. mov al,[esi] ; indirect memory access, load 1 byte

19. mov bx,[esi] ; indirect memory access, load 1 word

20. mov ecx,[esi] ; indirect memory access, load 1 double-word

21. mov edi,[data3] ; direct memory operand

Page 52: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

52

Memory Operands (1/2)

° The Effective Address of memory operands are computed by means of a segment selector and an offset.

° The segment selector can be specified either implicitly or explicitly:

• the most common method of specifying a segment selector is to load it in a segment register and then allow the processor to select the register implicitly, depending on the type of operation being performed.

° Default Segment Selection Rules:• CS: instruction fetches

JMP _MAIN

• SS: stack pushes & pops; references using ESP & EBPPUSH EAX

• DS: data references, except when relative to stackMOV EAX,[DATA]

• ES: destination of string operations

Page 53: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

53

Memory Operands (2/2)° Offset calculation:

[Base] + [Index]*Scale factor + Displacement• Displacement: An 8-, 16-, or 32-bit value.

• Base: the value in a general-purpose register.

• Index: the value in a general-purpose register.

• Scale factor: a value of 2, 4, or 8 that is multiplied by the index value.

8

4

Page 54: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

54

Contoh: memory-operand dalam tugas0a.asm*

1. segment .data

2. data1 db 11h

3. data2 dw 2222h

4. data3 dd 33333333h

5. datatmp times 9 db 0ffh

6. segment .bss

7. stacks resd 1

8. segment .text

9. global _asm_main

10. _asm_main:

11. mov eax,10 ; decimal number, value = 10

12. mov edx,eax ; register-to-register transfer

13. mov esi,data1 ; esi points to data1

18. mov al,[esi] ; indirect memory access, load 1 byte

19. mov bx,[esi] ; indirect memory access, load 1 word

20. mov ecx,[esi] ; indirect memory access, load 1 double-word

21. mov edi,[data3] ; direct memory operand

Page 55: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

55

Contoh: memory-operand [base+index*scale+disp]

struct Point {

int x;

int y;

} p[ ] = { {0,0}, {1,1} };

for (i=0; i<3; i++) {

x += p[i].x;

y += p[i].y;

}

_p dd 0, 0, 1, 1

...

mov ebx,_p

mov esi,_i

...

add eax,[ebx+8*esi+0]

add edx,[ebx+8*esi+4]

...

Page 56: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

56

DIRECTIVES

Page 57: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

57

SECTION or SEGMENT

° The SECTION (SEGMENT) directive changes which section of the output file the code you write will be assembled into.

° The Unix (coff, elf, ...) object formats, and the bin object format, all support the standardised section names:

• .text ; code’s segment

• .data ; (initialized) data’s segment

• .bss ; uninitialized data’s segment

Page 58: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

58

EXTERN & GLOBAL

° EXTERN is similar to the C keyword extern: it is used to declare a symbol which is not defined anywhere in the module being assembled, but is assumed to be defined in some other module and needs to be referred to by this one

• extern _printf

• extern _sscanf,_fscanf

° GLOBAL is the other end of EXTERN: if one module declares a symbol as EXTERN and refers to it, then in order to prevent linker errors, some other module must actually define the symbol and declare it as GLOBAL. Some assemblers use the name PUBLIC for this purpose.

° The GLOBAL directive applying to a symbol must appear before the definition of the symbol.

• global _main

• _main: ; some code

Page 59: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

59

Contoh: directives dalam tugas0a.asm*

1. segment .data

2. data1 db 11h

3. data2 dw 2222h

4. data3 dd 33333333h

5. datatmp times 9 db 0ffh

6. segment .bss

7. stacks resd 1

8. segment .text

9. global _asm_main

10. _asm_main:

11. mov eax,10 ; decimal number, value = 10

12. mov edx,eax ; register-to-register transfer

13. mov esi,data1 ; esi points to data1

18. mov al,[esi] ; indirect memory access, load 1 byte

19. mov bx,[esi] ; indirect memory access, load 1 word

20. mov ecx,[esi] ; indirect memory access, load 1 double-word

21. mov edi,[data3] ; direct memory operand

Page 60: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

60

PSEUDO-INSTRUCTIONS

Page 61: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

61

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

Page 62: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

62

RESB and friends: Declaring Uninitialised Data

° RESB, RESW, RESD, RESQ and REST are designed to be used in the BSS section of a module: they declare uninitialized storage space. Each takes a single operand, which is the number of bytes, words, doublewords or whatever to reserve.

buffer: resb 64 ; reserve 64 bytes

wordvar: resw 1 ; reserve a word

realarray: resq 10 ; array of ten reals

Page 63: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

63

EQU: Defining Constants

° EQU defines a symbol to a given constant value: when EQU is used, the source line must contain a label. The action of EQU is to define the given label name to the value of its (only) operand.

header_len equ 16

...

mov ecx,header_len ; eax 16

Page 64: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

64

TIMES: Repeating Instructions or Data

° The TIMES prefix causes the instruction to be assembled multiple times.

zerobuf: times 64 db 0

...

times 100 movsb

Page 65: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

65

Contoh: pseudo-instructions dalam tugas0a.asm*

1. segment .data

2. data1 db 11h

3. data2 dw 2222h

4. data3 dd 33333333h

5. datatmp times 9 db 0ffh

6. segment .bss

7. stacks resd 1

8. segment .text

9. global _asm_main

10. _asm_main:

11. mov eax,10 ; decimal number, value = 10

12. mov edx,eax ; register-to-register transfer

13. mov esi,data1 ; esi points to data1

18. mov al,[esi] ; indirect memory access, load 1 byte

19. mov bx,[esi] ; indirect memory access, load 1 word

20. mov ecx,[esi] ; indirect memory access, load 1 dword

21. mov edi,[data3]; direct memory operand

Page 66: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

66

EXPRESSIONS

Page 67: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

67

Special Expressions

° NASM supports two special tokens in expressions, allowing calculations to involve the current assembly position: the $ and $$ tokens.

° $ evaluates to the assembly position at the beginning of the line containing the expression;

message db 'hello, world'

msglen equ $-message

buffer: db 'hello, world' times 64-$+buffer db ' ‘

• so you can code an infinite loop using JMP $.

° $$ evaluates to the beginning of the current section;

• so you can tell how far into the section you are by using ($-$$).

Page 68: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

68

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

Page 69: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

69

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

6. segment .bss

7. stacks resd 1

8. segment .text

9. global _asm_main

10. _asm_main:

11. mov eax,~0xEF&0xFF ; decimal number, value = 10

12. mov edx,eax ; register-to-register transfer

13. mov esi,data1 ; esi points to data1

18. mov al,[esi] ; indirect memory access, load 1 byte

19. mov bx,[esi] ; indirect memory access, load 1 word

20. mov ecx,[esi] ; indirect memory access, load 1 dword

21. mov edi,[data3]; direct memory operand

Page 70: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

70

CONTOH PROGRAM

Page 71: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

71

hello.asm

extern _printf

segment .data

the_str db "hello world", 10, 0

segment .text

global _asm_main

_asm_main:

enter 0,0

pusha

push dword the_str

call _printf ; printf(“hello world\n”)

pop eax

popa

mov eax,0 ; return back to main() – driver.c

leave

ret

Page 72: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

72

EVALUASI TUGAS0

Page 73: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 04: Assembly Language Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

73

tugas0a.asm1. segment .data

2. data1 db 11h

3. data2 dw 2222h

4. data3 dd 33333333h

5. data4 times 16 db 0

14. mov eax,10 ; eax =

15. mov ebx,10b ; ebx =

16. mov ecx,10h ; ecx =

17. mov edx,eax ; edx =

24. mov esi,data1 ; esi =

25. mov al,[esi] ; eax =

26. mov bx,[esi] ; ebx =

27. mov ecx,[esi] ; ecx =

28. mov edx,[data1] ; edx =

29. mov esi,[data2] ; esi =

30. mov edi,[data3] ; edi =

31. mov [data4],dl ; [data4] =

32. mov [data4],dx ; [data4] =

33. mov [data4],edx ; [data4] =

0x0000000a

0x00000002

0x00000010

0x0000000a

0x0000c8d0; [0x0000c8d0] = 0x11, 0x22, 0x22, 0x33

0x33, 0x33, 0x33, 0xff

0x00000011

0x00002211

0x33222211

0x33222211

0x33332222

0x33333333

0x11, 0x00, 0x00, 0x00; data4=0x0000c8e0

0x11, 0x22, 0x00, 0x00

0x11, 0x22, 0x22, 0x33


Top Related