29
1 CSE 2312 Lecture 18 Addressing and Subroutine Computer Organization & Assembly Language Programming

Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

1

CSE 2312

Lecture 18 Addressing and Subroutine

Computer Organization &

Assembly Language Programming

Page 2: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

2

Sections in 8088 Code

– TEXT section, for the processor instructions.

– DATA section for the initialization of the memory in thedata segment, which is known at the start of the process.

– BSS (Block Started by Symbol), section, for thereservation of memory in the data segment that is notinitialized (i.e., initialized to 0).

– Each of these sections has its own location counter. Atrun time, the TEXT section is stored in the text segmentand the data and BSS sections are stored(consecutively) in the data segment.

Page 3: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

3

Label

• Label– An instruction or data word in the assembly language program can

begin with a label.– A label may also appear all by itself on a line, in which case it is as

though it appeared on the next instruction or data word.– For example, L is a label that refers to the instruction of data word

following it.

• Different Kinds of Labels– Global labels: alphanumeric identifiers followed by a colon (:). These

must all be unique, and cannot match any keyword or instructionmnemonic.

– Local labels: in the TEXT section only, we can have local labels,each of which consists of a single digit followed by a colon (:). Alocal label may occur multiple times.

Page 4: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

4

Constants

• Constants

– The assembler allows constants to be given a symbolic nameusing the syntax identifier

expression identifier = expression

– The identifier is an alphanumeric string, as in BLOCKSIZE =1024

– Like all identifiers in this assembly language, only the firsteight characters are significant, so BLOCKSIZE andBLOCKSIZZ are the same symbol, namely, BLOCKSIZ.

– Expressions can be constructed from constants, numericalvalues, and operators.

– Labels are considered to be constants because at the end ofthe first pass their numerical values are known.

Page 5: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

5

Pseudoinstructions

• The SPACE Pseudoinstructions

– The SPACE pseudoinstruction simply requires the locationpointer to be incremented by the number of bytes given in thearguments.

– This keyword is especially useful following a label in the BSSsegment to reserve memory for a variable.

• The ALIGN Pseudoinstructions– The ALIGN keyword is used to advance the location pointer to the

first 2-, 4-, or 8-byte boundary in memory to facilitate the assemblyof words, longs, etc. at a suitable memory location.

• The EXTERN Pseudoinstructions– EXTERN announces that the routine or memory location mentioned

will be made available to the linker for external references.

– The definition need not be in the current file; it can also besomewhere else, as long as the linker can handle the reference.

Page 6: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

6

8088 Memory

• Memory Organization– The 8088 has a somewhat ungainly memory organization due to its

combination of a 1-MB memory and 16-bit registers.

– With a 1-MB memory, it takes 20 bits to represent a memoryaddress. However, it is impossible to store a pointer to memory inany of the 16-bit registers.

– To get around this problem, memory is organized as segments,each of them 64 KB, so an address within a segment can berepresented in 16 bits.

• Memory Segment– The memory of the 8088, which consists simply of an array of

addressable 8-bit bytes, is used for the storage of instructions aswell as for the storage of data and for the stack.

– In order to separate the parts of the memory which are used forthese different purposes, the 8088 uses segments which are chunksof the memory set apart for a certain uses.

– In the 8088, such a segment consists of 65,536 consecutive bytes.

Page 7: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

7

Memory Segments

• Four Segments– The code segment

– The data segment

– The stack segment

– The extra segment

• Code Segment– The code segment contains the program instructions. The contents of

the PC register are always interpreted as a memory address in thecode segment.

– A PC value of 0 refers to the lowest address in the code segment, notabsolute memory address zero.

• Data Segment– The data segment contains the initialized and uninitialized data for the

program.

– When BX contains a pointer, it points to this data segment.

• Extra Segment– The extra segment is a spare segment register that can be placed

anywhere in memory that it is needed.

Page 8: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

8

Stack Segment

• Stack Segment– The stack segment contains local variables and intermediate results

pushed on the stack.

– Addresses in SP and BP are always in this stack segment.

• POP and PUSH

– The stack segment is made up of 2-byte words and so the stackpointer, SP, should always contain an even number.

– The stack is filled up from high addresses to low addresses. Thus, thePUSH instruction decreases the stack pointer by 2 and then stores theoperand in the memory address computed from SS and SP.

– The POP command retrieves the value, and increments SP by 2.Addresses in the stack segment which are lower than those indicatedby SP are considered free.

– Stack cleanup is thus achieved by merely increasing SP.

– In practice, DS and SS are always the same, so a 16-bit pointer can beused to refer to a variable in the shared data/stack segment.

– If DS and SS were different, a 17th bit would be needed on eachpointer to distinguish pointers into the data segment from pointers intothe stack segment.

Page 9: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

9

Segment Registers

• Address– For each of the segments, there exists a corresponding segment

register: the 16-bit registers CS, DS, SS, and ES.

– The starting address of a segment is the 20-bit unsigned integer. It isconstructed by shifting the segment register by 4 bits to the left, andputting zero’s in the four rightmost positions.

– This means that segment registers always indicate multiples of 16, in a20-bit address space. The segment register points to the base of thesegment.

– Addresses within the segment can be constructed by converting the 16-bit segment register value to its true 20-bit address by appending fourzero bits to the end and adding the offset to that.

– In effect, an absolute memory address is computed by multiplying thesegment register by 16 and then adding the offset to it.

– For example, if DS is equal to 7, and BX is 12, then the addressindicated by BX is 7 × 16 + 12 =124.

– In other words, the 20-bit binary address implied by DS = 7 is00000000000001110000. Adding the 16-bit offset 0000000000001100(decimal 12) to the segment’s origin gives the 20-bit address00000000000001111100 (decimal 124).

Page 10: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

10

Memory Reference

• Absolute Address– For every memory reference, one of the segment registers is

used to construct the actual memory address.

– If some instruction contains a direct address without reference toa register, then this address is automatically in the data segment,and DS is used to determine the base of the segment.

– The physical address is found by adding this bottom to theaddress in the instruction.

– The physical address in memory of the next instruction code isobtained by shifting the contents of CS by four binary places andadding the value of the program counter.

– In other words, the true 20-bit address implied by the 16-bit CSregister is first computed, then the 16-bit PC is added to it toform a 20-bit absolute memory address.

Page 11: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

11

Addressing: Instruction

• Destination and Source– Almost every instruction needs data, either from memory or from the

registers.

– To name this data, the 8088 has a reasonably versatile collection ofaddressing modes.

– Many instructions contain two operands, usually called destinationand source.

• For Instance– The copy instruction, MOV AX, BX;

– The add instruction: ADD CX, 20

– In them, the first operand is destination and the second is thesource. (The choice of which goes first is arbitrary; the reversechoice could also have been made.)

– The destination must be a left value that is, it must be a place wheresomething can be stored. This means that constants can besources, but not destinations.

Page 12: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

12

Addressing: 8088 Design

• Original design– In its original design, the 8088 required that at least one operand in

a two operand instruction be a register.

– This was done so that the difference between word instructions, andbyte instructions could be seen by checking whether the addressedregister was a word register or a byte register.

– In the first release of the processor, this idea was so strictly enforcedthat it was even impossible to push a constant, because neither thesource nor the destination was a register in that instruction.

• Later Version– Later versions were less strict, but the idea influenced the design

anyway.

– In some cases, one of the operands is not mentioned. For example,in the MULB instruction, only the AX register is powerful enough toact as a destination.

Page 13: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

13

Addressing Mode

• One Operand– There are also a number of one-operand instructions, such as increments,

shifts, negates, etc.

– In these cases, there is no register requirement, and the difference betweenthe word and byte operations has to be inferred from the opcodes (i.e.,instruction types) only.

• Basic Data Types– The 8088 supports four basic data types: 1-byte byte, the 2-byte word, the 4-

byte long, and binary coded decimal.

– In the binary coded decimal, two decimal digits are packed into a word. It isnot supported by the interpreter.

– A memory address always refers to a byte, but in case of a word or a long, thememory locations directly above the indicated byte are implicitly referred toas well.

– The word at 20 is in the memory locations 20 and 21. The long at address 24occupies the addresses 24, 25, 26 and 27.

– In the stack segment, words should be placed at even addresses.

Page 14: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

14

Operand Addressing Mode (1)

The Table gives an overview of the 8088 addressing modes.

The topmost horizontal block of the table lists the registers.

They can be used as operands in nearly all instructions, both as sources and as

destinations.

There are eight word registers and eight byte registers.

Page 15: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

15

Operand Addressing Mode (2)

Operand addressing modes.

The symbol # indicates a numerical value or label.

Page 16: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

16

Data Segment Addressing (1)

• Data Segment Addressing– Data segment addressing contains addressing modes for the data segment.

– Addresses of this type always contain a pair of parentheses, to indicate thatthe contents of the address instead of the value is meant.

• Addressing Modes

– Direct Addressing

– Register Indirect Addressing

– Register Displacement Addressing

– Register with Index

– Register with Index and Displacement

Page 17: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

17

Data Segment Addressing (2)

• Direct Addressing

– The easiest addressing mode of this type is direct addressing, in which thedata address of the operand is in the instruction itself.

– Example: ADD CX, (20), in which the contents of the memory word ataddress 20 and 21 is added to CX.

– Memory locations are usually represented by labels instead of by numericalvalues in the assembly language, and the conversion is made at assembly time.

– The parentheses around the labels are essential (for the assembler we areusing) because ADD CX,20 is also a valid instruction, only it means add theconstant 20 to CX, not the contents of memory word 20.

– In the above Table, the # symbol is used to indicate a numerical constant,label, or constant expression involving a label.

• Register Indirect Addressing– In register indirect addressing, the address of the operand is stored in one of

the registers BX, SI, or DI.

– In all three cases the operand is found in the data segment.

Page 18: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

18

Data Segment Addressing (3)

• Register Displacement Addressing– Put a constant in front of the register. Then the address is found by adding

the register to the constant. It is called register displacement, is convenient forarrays.

– For example, SI contains 5, then the fifth character of the string at the labelFORMAT can be loaded in AL by MOVB AL, FORMAT(SI).

• Register with Index– The entire string can be scanned by incrementing or decrementing the

register in each step.– It is also possible to put the base (i.e., lowest numerical address) of the array

in the BX register, and keep the SI or DI register for counting. This is calledregister with index addressing.

– For example PUSH (BX)(DI) fetches the contents of the data segmentlocation whose address is given by the sum of the BX and DI registers. Thisvalue is then pushed onto the stack.

• Register with Index and Displacement– We can combine the Register Displacement Addressing mode and the

Register Displacement Addressing mode together to get register with indexand displacement addressing.

– For example, NOT 20(BX)(DI), which logistically complements thememory word at BX + DI + 20 and BX + DI + 21.

Page 19: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

19

Stack Segment Addressing

• Stack Segment– All the indirect addressing modes in the data segment also exist for the stack

segment.

– In this case, the base pointer BP is used instead of the base register BX.

– In this way (BP) is the only register indirect stack addressing mode, but moreinvolved modes also exist, up to base pointer indirect with index anddisplacement −1(BP)(SI).

– These modes are valuable for addressing local variables and functionparameters, which are stored in stack addresses in subroutines.

Page 20: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

20

Effective&Immediate&Implied Addressing

• Effective Addressing– All the addresses which comply with the addressing modes discussed up to

now can be used as sources and as destinations for operations.– Together they are defined to be effective addresses.– The addressing mode in the remaining two blocks cannot be used as

destinations and are not referred to as effective addresses. They can only beused as sources.

• Immediate Addressing– The addressing mode in which the operand is a constant byte or word value

in the instruction itself is called immediate addressing.– For example, CMP AX, 50 compares AX to the constant 50 and sets bits in

the flag register, depending on the results.

• Implied Addressing– In this case, the operand or operands are implicit in the instruction itself.– The instruction PUSH AX pushes the contents of AX onto the stack by

decrementing SP and then copying AX to the location now pointed to by SP.– SP is not named in the instruction itself, however; the fact that it is a PUSH

instruction implies that SP is used.– Flag related instructions implicitly use status flags register without naming it.

Page 21: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

21

Automatic Increment and Decrement

• Auto Increment/Decrement Mode– The 8088 has special instructions for moving (MOVS), comparing (CMPS),

and scanning (SCAS) strings.

– With these string instructions, the index registers SI and DI are automaticallychanged after the operation.

– This behavior is called auto increment or auto decrement mode.

– Whether SI and DI are incremented or decremented depends on thedirection flag in the status flags register.

• For Example– A direction flag value of 0 increments, whereas a value of 1 decrements. The

change is 1 for byte instructions and 2 for word instructions.

– The stack pointer is also auto increment and auto decrement: it isdecremented by 2 at the start of a PUSH and incremented by 2 at the end ofa POP.

Page 22: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

22

Subroutine Calls

• Subroutine– The 8088 has an instruction used to call procedures, usually known in

assembly language as subroutines.– The destination is either a label or can be found at an effective address.

Parameters have to be pushed onto the stack in reverse order first.– In assembly language, parameters are usually called arguments, but the terms

are interchangeable. Following these pushes the CALL instruction isexecuted.

– The instruction starts by pushing the current program counter onto the stack.In this way the return address is saved.

– The return address is the address at which the execution of the calling routinehas to be resumed when the subroutine returns.

– Next the new program counter is loaded either from the label, or from theeffective address.

– The return instruction, RET, just pops the return address from the stack,stores it in the program counter and the program continues at the instructionimmediately after the CALL instruction.

– Sometimes the RET instruction contains a positive number as immediatedata. This number is assumed to be the number of bytes of arguments thatwere pushed onto the stack before the call; it is added to SP to clean up thestack.

Page 23: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

23

Subroutine Calls

An example stack.

Page 24: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

24

Subroutine Example

• Example ( vecprod.s pp. 732)– Compute the inner product of two vector

– vecmul(count, vec1, vec2)

Page 25: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

The program vecprod.s.

Page 26: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

Figure C-14. The program vecprod.s (cont’d)

Page 27: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

27

Jump Instructions

• JMP

– JMP is an unconditional jump to a label that is usually within the sameprocedure.

– Syntax: JMP target

– Logic: EIP target

– Example:

top:

.

.

jmp top

A jump outside the current procedure must be to a special type of label

called a global label

Page 28: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

28

Conditional Jumps

Page 29: Computer Organization & Assembly Language Programmingranger.uta.edu/~sjiang/CSE2312-spring-17/CSE2312_Lecture... · 2017. 4. 24. · 8088 Memory • Memory Organization – The 8088

29

Program: MY_MAX

_EXIT = 1 ! 1

_PRINTF = 127 ! 2

.SECT .TEXT ! 3

start: ! 4 ===== Bubble sort on AH, AL, BH, BL ======

MOV AX, 514 ! 5 AX = 514

MOV BX, 1025 ! 6 BX = 1025

PUSH BX ! 7 2nd parameter of MY_MAX: b

PUSH AX ! 8 1st parameter of MY_MAX: a

CALL MY_MAX ! 9 call (AX, BX)=MY_MAX(a, b); // use MY_MAX function to find the min and max

ADD SP, 4 ! 10 Clean up stack

output2: ! 11 output2 is the same as output1

PUSH BX ! 12 push BX for printf, param 3

PUSH AX ! 13 push AX for printf, param 2

PUSH pfmf2 ! 14 push format for printf, param 1

PUSH _PRINTF ! 15 push system call ID of printf

SYS ! 16 make system call

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

POP AX ! 18 store AX

POP BX ! 19 store BX

exit: ! 20 Exit

PUSH 0 ! 20 Just return success(0)

PUSH _EXIT ! 21 system call id exit

SYS ! 22 make system call

MY_MAX: ! ret max store in AX ! 24 function (AX, BX) = MY_MAX(a, b)

PUSH BP ! 25 save old value of BP on stack

MOV BP, SP ! 26 BP = SP, means start a new frame for the function

! process ! 27

MOV AX, 4(BP) ! 28 AX=a

MOV BX, 6(BP) ! 29 BX=b

CMP BX, AX ! 30 Compare a and b

JL RETURN ! 31 if (b<a) goto RETURN;

XCHG AX, BX ! 32 else swap(a, b);

RETURN: ! 33

POP BP ! 34 restore BP to previous value

RET ! 35 AX = max, BX = min

.SECT .DATA ! 36

pfmf2: ! 37

.ASCIZ "AX:%d, BX:%d\n" ! 38

.SECT .BSS ! 39