Computer Organization & Assembly Language...

29
1 CSE 2312 Lecture 13 Registers, Address modes, and Instructions in Intel 8088 Processor Computer Organization & Assembly Language Programming

Transcript of Computer Organization & Assembly Language...

Page 1: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

1

CSE 2312

Lecture 13

Registers, Address modes, and Instructions in Intel 8088 Processor

Computer Organization &

Assembly Language Programming

Page 2: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

2

Registers in the 8088 Processor

• Registers in the 8088 Processor– Every processor, including the 8088, has an internal state, where it

keeps certain crucial information.

– Thus, the processor has a set of registers where this informationcan be stored and processed.

– Probably the most important of these is the PC (program counter),which contains the memory location, that is, the address, of the nextinstruction to be executed.

– This register is also called IP (Instruction Pointer). This instruction islocated in a part of the main memory, called the code segment.

• Different Registers– General registers

– Segment registers

– Pointer and index registers

– Condition code register or Flag register

– Instruction pointers (program counter)

Page 3: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

3

General Registers

Page 4: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

4

General Registers

• Several different general registers– The registers AX, BX, CX, and DX are the general registers.

– The registers AH, AL, BH, BL, CH, CL, DH, DL are the generalregisters.

• Accumulator Register AX– AX is one of the general registers, which is called the accumulator

register.

– Used to collect results of computations

– The target of many of the instructions.

– AX is often the implied destination, for example, in multiplication.

• Base Register BX– BX is the base register.

– For many purposes BX can be used in the same way as AX, but BXcan hold a pointer to memory, AX cannot.

– MOV AX,BX which copies to AX the contents of BX. (BX contains thesource operand)

– MOV AX,(BX) which copies to AX the contents of the memory wordwhose address is contained in BX. (BX points to the source operand)

Page 5: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

5

General Registers (cont’d)

• Counter Registers CX– Besides fulfilling many other tasks, this register is specifically used to

contain counters for loops.

– It is automatically decremented in the LOOP instruction, and loops areusually terminated when CX reaches zero..

• Data Register DX– It is used together with AX in double word length (i.e., 32-bit)

instructions.

– In this case, DX contains the high-order 16 bits and AX contains thelow-order 16 bits. Usually, 32-bit integers are indicated by the termlong.

• Eight 8-bit Registers– All of these general registers can be regarded either as a 16-bit register

or as a pair of 8-bit registers. E.g., AX can be regarded as AH and AL.

– In general, instructions doing arithmetic use the full 16-bit registers, butinstructions dealing with characters usually use the 8-bit registers.

– AL and AH are just names for both halves of AX. When AX is loadedwith a new value, both AL and AH are changed to the lower and upperhalves of the 16-bit number put in AX, respectively.

Page 6: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

6

Exercise

• A Simple Example

– What is the value in AX after executing the followinginstructions, where 258 is a decimal number?

MOV AX, 258

ADDB AH, AL

• Solution– (258)10 = (0000 0001 0000 0010)2

– MOV AX, 258 AX= (0000 0001 0000 0010)2

– AH= (0000 0001)2 AL = (0000 0010)2

– ADDB AH, AL AH= (0000 0011)2

– AX= (0000 0011 0000 0010)2 = (770)10

Page 7: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

7

Pointer Registers

• Pointer or Index Registers– Stack pointer, Base pointer, Source pointer, Destination pointer

• Stack Pointer (SP)– The stack is a segment of memory that holds certain context

information about the running program. Usually, when a procedure iscalled, part of the stack is reserved for holding the procedure’s localvariables, the address to return to when the procedure has finished,and other control information.

– The portion of the stack relating to a procedure is called its stack frame.When a called procedure calls another procedure, an additional stackframe is allocated, usually just below the current one. Additional callsallocate additional stack frames below the current ones.

– Stacks almost always grow downward, from high addresses to lowaddresses. But the lowest numerical address occupied on the stack isalways called the top of the stack.

– PUSH puts a 16-bit word on top of the stack. This instruction firstdecrements SP by 2, then stores its operand at the address SP is nowpointing to. POP removes a 16-bit word from the top of the stack byfetching the value on top of the stack and then incrementing SP by 2.

– The SP register points to the top of the stack and is modified by PUSH,POP, and CALL instructions, being decremented by PUSH,incremented by POP, and decremented by CALL.

Page 8: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

8

Pointer Registers (cont’d)

• Base Pointer (BP)– It usually contains an address in the stack. Whereas SP always

points to the top of the stack, BP can point to any location within thestack.

– In practice, a common use for BP is to point to the beginning of thecurrent procedure’s stack frame, in order to make it easy to find theprocedure’s local variables.

– Thus, BP often points to the bottom of the current stack frame (thestack frame word with the highest numerical value) and SP points tothe top (the stack frame word with the lowest numerical value).

– The current stack frame is thus delimited by BP and SP.

• Source Index (SI) and Destination Index (DI)– SI, the source index, and DI, the destination index.

– These registers are often used in combination with BP to addressdata in the stack, or with BX to compute the addresses of datamemory locations.

Page 9: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

9

Instruction Pointer and Segment Register

• Instruction Pointer (PC)– The instruction pointer has the Intel’s name: program counter (PC).

– This register is not addressed directly by the instructions, but containsan address in the program code segment of the memory.

– The instruction cycle starts by fetching the instruction pointed to by PC.This register is then incremented before the rest of the instruction isexecuted.

– So, it points to the first instruction beyond the current one.

• Segment Register– The stack, the data and the instruction codes all reside in main

memory, but usually in different parts of it. The segment registersgovern these different parts of the memory, which are called segments.

– There are four registers in the segment register group.

– CS for the code segment register; DS for the data segment register,

– SS for the stack segment register; ES for the extra segment register.

– Most of the time, their values are not changed.

– In practice, the data segment and stack segment use the same piece ofmemory, with the data being at the bottom of the segment and the stackbeing at the top.

Page 10: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

10

Flag Register

– The flag register (condition code register) is a set of single-bitregisters.

– Some of the bits are set by arithmetic instructions and relate to thecodes:

Z - result is zero

S - result is negative (sign bit)

V - result generated an overflow

C - result generated a carry

A - Auxillary carry (out of bit 3)

P - parity of the result

– Other bits in this register control operation of certain aspects of theprocessor.

– The I bit enables interrupts.

– The T bit enables tracing mode, which is used for debugging.

– Finally, the D bit controls the direction of the string operations.

– Not all 16 bits of this flag register are used;

– The unused ones are hardwired to zero.

Page 11: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

11

Operand Addressing Modes

(The symbol # indicates a numerical value or label)

Page 12: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

12

Operand Addressing Modes

(The symbol # indicates a numerical value or label)

Page 13: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

13

Some 8088 Instructions (see Fig. C-4 for more)

Page 14: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

14

Data Transfer Instructions

• Data Transfer Instructions– MOV or MOVB

– XCHG or XCHGB

– PUSH or POP

– PUSHF or POPF

– XLAT

Page 15: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

15

Move Instructions

• Copy and Move Instructions– Move from source to destination. Syntax: MOV destination, source– If the source is a register, the destination can be an effective address. In

this table a register operand is indicated by an r and an effectiveaddress by an e, so this operand combination is denoted by e←r.

– Since, in the instruction syntax, the destination is the first operand andthe source is the second operand, the arrow ← is used to indicate theoperands. Thus, e←r means that a register is copied to an effectiveaddress.

– For the MOV instruction, the source can also be an effective addressand the destination a register, which will be denoted by r←e.

– Another choice is immediate data as source, and effective address asdestination, which yields e←#. Immediate data in the table is indicatedby the sharp sign (#).

– The Move instructions do not move data. They make copies, meaningthat the source is not modified as would happen with a true move.

• MOV or MOVB– word move MOV and the byte move MOVB– No more than one memory operand permitted– CS and IP cannot be the destination– No immediate to segment moves

Page 16: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

16

Example

• MOV DS, 45

– Explain why it is invalid?

– Immediate move to DS not permitted

• MOV 25, VAR

– Explain why it is invalid?

– Immediate value cannot be destination

• MOV AX, BH

– Explain why it is invalid?

– The sizes are missed to match

Page 17: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

17

XCHG Instruction

• XCHG– Exchanges the contents of a register with the contents of an effective

address.

– For the exchange, the table uses the symbol ↔. In this case, thereexists a byte version as well as a word version.

– Thus, the instruction is denoted by XCHG and the Operand fieldcontains r↔e.

– At least one operand must be a register.

– No immediate operands are permitted.

– Example:

XCHG AX, BX ! Exchange 16-bit registers

XCHGB AH, AL ! Exchange 8-bit registers

XCHG VAR1, BX ! Exchange memory and register

XCHG VAR1, VAR2 ! Error: two memory operands

Page 18: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

18

Example

• Problem– Suppose AX=1, BX=2, CX=3; Write a program that rearranges their values such

that AX=3; BX=1; CX=2.

• Solution– Step1: Exchange the values in AX and CX.

XCHG AX, CX

– Step2: Exchange the values in BX and CX.

XCHG BX, CX

Page 19: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

19

PUSH and POP

• PUSH– PUSH pushes its operand onto the stack.

– The explicit operand can either be a constant (# in the Operandscolumn) or an effective address (e in the Operands column).

– There is also an implicit operand, SP, which is not mentioned in theinstruction syntax.

– What the instruction does is decrement SP by 2, then store the operandat the location now pointed to by SP.

• POP– POP removes an operand from the stack to an effective address.

– What the instruction does is increment SP by 2, then store the operandat the location now pointed to by SP.

• PUSHF and POPF– They have implied operands, the push and pop the flags register,

respectively.

– This is also the case for XLAT which loads the byte register AL from theaddress computed from AL + BX. This instruction allows for rapidlookup in tables of size 256 bytes.

Page 20: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

20

ADD and SUB Instructions

• ADD and SUB– ADD destination, source; Logic: destination destination + source

– SUB destination, source; Logic: destination destination – source

• Operand Rules– Each of these has the same three operand combinations as MOV

– Effective address to register, register to effective address, and constantto effective address. Thus, the Operands column of the table containsr←e, e←r, and r←#.

– In them, the overflow flag, O, the sign flag, S, the zero flag, Z, and thecarry flag, C are all set, based on the result of the instruction.

– For example, that O is set if the result cannot be correctly expressed inthe allowed number of bits, and cleared if it can be.

– When the largest 16-bit number, 0x7fff (32,767 in decimal), is added toitself, the result cannot be expressed as a 16-bit signed number, so O isset to indicate the error.

– For all additions and subtractions, byte versions also exist.

Page 21: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

21

Execise

.SECT .TEXT

MOV AX,1028

MOV BX,4

ADD AX,BX

ADDB AH,AL

! --AX--

! 1028

! 4

! 1032

! 3080

Page 22: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

22

MUL and DIV Instructions

• Operand Rules– Unsigned integer operands use the MUL and DIV instructions;– AH:AL combination is the implied destination in the byte version.– DX:AX combination is the implied destination in the word version.– Even if the result is only a word/byte, the DX/AH register is rewritten .

• Multiplication– It is always possible because the destination contains enough bits.– The overflow/carry bits are set if the product is over one word/byte.– The zero and and negative flags are undefined after a multiply.

• Division– Uses the register combinations DX:AX or AH:AL as the destination.– The quotient goes into AX or AL and the remainder into DX or AH.– All four flags, carry, overflow, zero and negative, are undefined after a

divide operation.– If the divisor is 0, it executes a trap to stop the program unless a trap

handler routine is present.– It is sensible to handle minus signs before and after the divide, because

in the 8088 definition the sign of the remainder equals the sign of thedividend, whereas in mathematics, a remainder is always nonnegative.

Page 23: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

23

MUL Instruction

• MUL– The MUL (unsigned multiply) instruction multiplies an 8-, 16-, or 32-bit operand

by either AL, AX, or EAX.

• Format– MUL r/m8

– MUL r/m16

– MUL r/m32

Page 24: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

24

MUL Example

.SECT .TEXT

MOV AX,257 ! AX=257

MOV BX,4 ! BX=4

MOV CX,2 ! CX=2

MUL BX ! [DX:AX]=AX*BX

MUL BL ! [AH:AL]=AL*BL

MOV AX, CX ! AX=CX

MUL BX ! [DX:AX]=AX*BX

The Carry flag indicates whether or

not the upper half of the product

contains significant digits.

! --AX-- --BX-- --CX--

! 257 0 0

! 257 4 0

! 257 4 2

! 1028 4 2

! 16 4 2

! 2 4 2

! 8 4 2

Page 25: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

25

DIV Instruction

• DIV– The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and 32-bit division

on unsigned integers

– A single operand is supplied (register or memory operand), which is assumed tobe the divisor

– Instruction formats:

DIV reg/mem8

DIV reg/mem16

DIV reg/mem32

Page 26: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

26

DIV Example

.SECT .TEXT

MOV AX,1029 ! AX=1029

MOV BX,4 ! BX=4

MOV CX,2 ! CX=2

DIV BX ! DX:AX = AX/BX

DIVB BL ! AH:AL=AL/BL

DX:AX = [DX:AX]/BX, where DX is the remainder, AX is the quotient

AH:AL = [AH:AL}/BL, where AH is the remainder, AL is the quotient

! --AX-- --BX-- --CX--

! 1029 0 0

! 1029 4 0

! 1029 4 2

! 257 4 2

! 320 4 2

Page 27: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

27

Example Program 1

_EXIT = 1 ! 1

_PRINTF = 127 ! 2

.SECT .TEXT ! 3 ===== Begin of code segment =====

start: ! 4

! Step 1) ! 5 initialize AX and BX using MOV

MOV AX, 514 ! 6 AX = 514

MOV BX, 2 ! 7 BX = 2

! 8

output1: ! 9 Line 4~12 is to printf("%d, %d\n", AX, BX);

PUSH BX ! 10 push BX for printf, param 3

PUSH AX ! 11 push AX for printf, param 2

PUSH pfmf2 ! 12 push format string for printf, param 1

PUSH _PRINTF ! 13 push system call id of printf

SYS ! 14 make system call

ADD SP, 4 ! 15 clean up 2 word of stack

POP AX ! 16 store AX: *Because printf will return number of string in AX

POP BX ! 17 store BX: This statement just for clean the stack

PUSH 0 ! 18 Just return success(0)

PUSH _EXIT ! 19 system call id exit

SYS ! 20 make system call

! 21

.SECT .DATA ! 22 ===== Begin of data segment =====

pfmf2: ! 23

.ASCIZ "%d, %d\n" ! 24 Define format string for print two 16bit integer

.SECT .BSS ! 25 ===== Begin of bss segment =====

Page 28: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

28

Example Program 2

_EXIT = 1 ! 1

_PRINTF = 127 ! 2

.SECT .TEXT ! 3 ===== Begin of code segment =====

start: ! 4

MOV AX, 513 ! 5 initialize AX = 514

MOV BX, 1 ! 6 initialize BX = 2

ADDB BH, BL ! 7 8bit add: BH = BH + BL

SUBB AH, AL ! 8 8bit add: AH = AH - AL

PUSH BX ! 9 Line 18~24 is to printf("%d, %d\n", AX, BX);

PUSH AX ! 10 push AX for printf

PUSH pfmf2 ! 11 push BX for printf, param 3

PUSH _PRINTF ! 12 push system call id of printf

SYS ! 13 make system call

ADD SP, 4 ! 14 clean up 2 word of stack

POP AX ! 15 If writing lab2 in one long program,

POP BX ! 16 should restore AX and BX for later use

exit: ! 17 Line 18~20: exit(0)

PUSH 0 ! 18 Just return success(0)

PUSH _EXIT ! 19 system call id exit

SYS ! 20 make system call

! 21

.SECT .DATA ! 22 ===== Begin of data segment =====

pfmf2: ! 25

.ASCIZ "%d, %d\n" ! 26 Define format string for print two 16bit integer

.SECT .BSS ! 42 ===== Begin of bss segment =====

Page 29: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-fall-17/CSE2312_Lecture13.pdf · Lecture 13 Registers, Address modes, and Instructions in Intel

29

Print

_EXIT = 1 ! 1

_PRINTF = 127 ! 2

.SECT .TEXT ! 3 ===== Begin of code segment =====

start: ! 4 init AX and BX

MOV AX, 2 ! 5 AX = 2

MOV BX, 514 ! 6 BX = 514

MUL BX ! 7 <DX:AX> = AX * BX

! 8 MUL is unsign 16bit multiplication, result is in DX:AX

output: ! 9 Line : printf("%d, %d, %d, %d\n", AH, AL, BH, BL);

MOV CX, 0 ! 10 CX = 0, clear CX

MOVB CL, BL ! 11 CL = BL

PUSH CX ! 12 Push the 8bit value of *BL* to the 16bit-based stack

MOVB CL, BH ! 13 CL = BH, similar to line 13;

PUSH CX ! 14 Push the 8bit value of *BH* to the 16bit-based stack

MOVB CL, AL ! 15 CL = AL, similar to line 13;

PUSH CX ! 16 Push the 8bit value of *AL* to the 16bit-based stack

MOVB CL, AH ! 17 CL = AH, similar to line 13;

PUSH CX ! 18 Push the 8bit value of *AH* to the 16bit-based stack

PUSH pfmf3 ! 19

PUSH _PRINTF ! 20

SYS ! 21 make the system call printf

ADD SP, 4 ! 22 clean stack for two words and Line 32~39 restore AX, BX

POP CX ! 23 CX = old AH, pushed in stack in Line 25

MOVB AH, CL ! 24 AH = CL

POP CX ! 25 CX = old AL, pushed in stack in Line 22

MOVB AL, CL ! 26 AL = CL

ADD SP, 4 ! 27 clean the stack

exit: ! 28 exit(0)

PUSH 0 ! 29 Just return success(0)

PUSH _EXIT ! 30 system call id exit

SYS ! 31 make system call

.SECT .DATA ! 32 ===== Begin of data segment =====

pfmf3: ! 33

.ASCIZ "%d, %d, %d, %d\n" ! 34 Define format string for print four 16bit integer

.SECT .BSS ! 35 ===== Begin of BSS segment =====