You are on page 1of 31

COMSATS Institute of
Information Technology
CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE
Asif Muhammad Malik
asif.malik@comsats.edu.pk

Quote of the day

TELL A LIE ONCE
AND
ALL YOUR TRUTHS
BECOME QUESTIONABLE !
4/17/16

CCS221– COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE

3

Quick recap 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 4 .

PTR. and LENGTHOF etc. OFFSET.Goals • This chapter introduces some essential instructions for transferring data and performing arithmetic using basic addressing modes • Direct • Immediate • indirect. which make it possible to process arrays • How to create loops • Use some basic operators.Chapter 4 . E.g. 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 5 .

[source-2] • There are three basic types of operands: • Register—uses a named register in the CPU e.[source-1].g. 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 6 . two. • Memory—references a memory location e. var1. • Immediate—uses a numeric literal expression e. 2 + 3 * 5 etc.bx etc.Operand Types • Instructions can have zero.g. ax. one.g.[source] mnemonic [destination]. or three operands • • • • mnemonic mnemonic [destination] mnemonic [destination]. var2 etc.

Standard Operand Types 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 7 .

data var1 4/17/16 BYTE 10h CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 8 .Direct Memory Operands • Variable names are references to memory within the data segment • Example. the following declaration for a variable named var1 says that its size attribute is byte and it contains the value 10 hexadecimal: .

source • Operand rules for MOV instruction: • Both operands must be the same size • Both operands cannot be memory operands • The instruction pointer register (EIP) cannot be a destination operand 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 9 .MOV Instruction • The MOV instruction copies data from a source operand to a destination operand (Known as a data transfer instruction) MOV destination.

mov var2. 4/17/16 var1 ax CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 10 . mem mem.data var1 WORD ? var2 WORD ? . imm • A single MOV instruction cannot be used to move data directly from one memory location to another • Instead. reg mem. reg reg. imm reg.MOV Instruction • List of the standard MOV instruction formats: • • • • • MOV MOV MOV MOV MOV reg.code mov ax. you must move the source operand’s value to a register first: .

oneWord . EAX = 00000078h mov ax. EAX = 12345678h 0 .data oneByte BYTE 78h oneWord WORD 1234h oneDword DWORD 12345678h .code mov eax.oneByte . 0 . EAX = 00000000h mov al. 4/17/16 .Overlapping Values • Example showing how the same 32-bit register can be modified using differently sized data . oneDword mov ax. EAX = 12340000h CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 11 . EAX = 00001234h mov eax.

0 mov cx.data count WORD 1 . count CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 12 .Copying Smaller Values to Larger Ones • MOV cannot directly copy data from a smaller operand to a larger one • Workaround: .code 4/17/16 mov ecx.

0 mov cx.Copying Smaller Values to Larger Ones • What happens if we try the same approach with a signed integer equal to 16? . signedVal .520) 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 13 . ECX = 0000FFF0h (+65.data signedVal SWORD -16 . FFF0h (-16) .code mov ecx.

signedVal . 0FFFFFFFFh mov cx. ECX = FFFFFFF0h (-16) • This technique is called sign extension 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 14 .Copying Smaller Values to Larger Ones • The value in ECX (+65.520) is completely different from -16 • Solution: If we had filled ECX first with FFFFFFFFh and then copied signedVal to CX. the final value would have been correct: mov ecx.

the first operand (a register) is the destination and the second is the source 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 15 . reg/mem8 • In each of the three variants.MOVZX Instruction • The MOVZX instruction (move with zero-extend) copies the contents of a source operand into a destination operand and zero-extends the value to 16 or 32 bits • This instruction is only used with unsigned integers • There are three variants: MOVZX reg32. reg/mem8 MOVZX reg32. reg/mem16 MOVZX reg16.

code movzx ax.MOVZX Instruction • The following example zero-extends binary 10001111 into AX: . byteVal . AX = 0000000010001111b 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 16 .data byteVal BYTE 10001111b .

bx movzx edx. . EAX = 0000A69Bh . byte1 . CX = 009Bh 4/17/16 byte1 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 17 .MOVZX Instruction • The following examples use registers for all operands.data byte1 BYTE 9Bh word1 WORD 0A69Bh . word1 . bl . EAX = 0000A69Bh movzx edx. CX = 009Bh • The following examples use memory operands for the source and produce the same results: . EDX = 0000009Bh movzx cx. 0A69Bh movzx eax. EDX = 0000009Bh movzx cx.code movzx eax. showing all the size variations: mov bx. bl .

reg/mem16 MOVSX reg16. reg/mem8 MOVSX reg32. reg/mem8 • An operand is sign-extended by taking the smaller operand’s highest bit and repeating (replicating) the bit throughout the extended bits in the destination operand 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 18 .MOVSX Instruction • The MOVSX instruction (move with sign-extend) copies the contents of a source operand into a destination operand and signextends the value to 16 or 32 bits • This instruction is only used with signed integers. There are three variants: MOVSX reg32.

byteVal .code movsx ax. AX = 1111111110001111b 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 19 .MOVSX Instruction (Example) .data byteVal BYTE 10001111b .

movsx eax. EAX = FFFFA69Bh CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 20 . the hexadecimal value moved to BX is A69B.MOVSX Instruction (Example) • A hexadecimal constant has its highest bit set if its most significant hexadecimal digit is greater than 7 • In the following example. 4/17/16 A69Bh bx . so the leading “A” digit tells us that the highest bit is set mov bx.

data saveflags BYTE ? .ah . Zero. load saved flags into AH . load flags into AH mov saveflags. Auxiliary Carry. sahf 4/17/16 saveflags .LAHF and SAHF Instructions • The LAHF (load status flags into AH) instruction copies the low byte of the EFLAGS register (Sign. and Carry) into AH . Parity. you can retrieve the values of flags saved earlier in a variable: mov ah. copy into Flags register CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 21 .code lahf . save them in a variable • The flags are left in the following order inside the register: SF ZF ?? AF ?? PF ?? CF • The SAHF (store AH into status flags) instruction copies AH into the low byte of the EFLAGS register • For example.

There are three variants: XCHG reg. ebx . mem XCHG mem. exchange 16-bit regs . al . exchange 32-bit regs 4/17/16 . bx xchg ah. reg XCHG reg. exchange 16-bit mem op with BX CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 22 . reg • The rules for operands in the XCHG instruction are the same as those for the MOV instruction.bx xchg eax.XCHG Instruction • The XCHG (exchange data) instruction exchanges the contents of two operands. except that XCHG does not accept immediate operands • Examples using XCHG: xchg ax. exchange 8-bit regs xchg var1.

AL = 20h • An expression such as arrayB+1 produces what is called an effective address by adding a constant to the variable’s offset • Surrounding an effective address with brackets makes it clear that the expression is dereferenced to obtain the contents of memory at the address • MASM has no built-in range checking for effective addresses.50h • If we use MOV with arrayB as the source operand. E. The result is a sneaky logic bug. creating a direct-offset operand.40h.30h. AL = ?? CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 23 .arrayB .[arrayB+1] . we automatically move the first byte in the array: mov al. AL = 10h • We can access the second byte in the array by adding 1 to the offset of arrayB: mov al.[arrayB+20] 4/17/16 .Direct-Offset Operands • You can add a displacement to the name of a variable. so be extra careful when checking array references: mov al.g. arrayB BYTE 10h.20h.

code mov ax.200h.300h .Direct-Offset Operands • Word and Doubleword Arrays: In an array of 16-bit words. the offset of each array element is 2 bytes beyond the previous one • Add 2 to ArrayW to reach the second element: . AX = 200h CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 24 . AX = 100h . arrayW mov ax.data arrayW WORD 100h. [arrayW+2] 4/17/16 .

Example Program (Moves) 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 25 .

Example Program (Moves) 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 26 .

Example Program (Moves) 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 27 .

Summary 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 28 .

IRVINE 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 29 .Reading Material • Chapter # 4 Assembly Language for x86 Processors. Seventh Edition By KIP R.