25
ECE291 Computer Engineering II Lecture 7 Josh Potts University of Illinois at Urbana- Champaign

ECE291 Computer Engineering II Lecture 7

  • Upload
    raanan

  • View
    21

  • Download
    0

Embed Size (px)

DESCRIPTION

ECE291 Computer Engineering II Lecture 7. Josh Potts University of Illinois at Urbana- Champaign. Outline. Multiplication Division Program Segment Prefix Command Line Parameters. Multiplication. The product after a multiplication is always a double-width product, e.g, - PowerPoint PPT Presentation

Citation preview

Page 1: ECE291 Computer Engineering II Lecture 7

ECE291Computer Engineering II

Lecture 7

Josh Potts

University of Illinois at Urbana- Champaign

Page 2: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Outline

• Multiplication• Division• Program Segment Prefix• Command Line Parameters

Page 3: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Multiplication

• The product after a multiplication is always a double-width product, e.g, – if we multiply two 16-bit numbers , they generate a 32-bit product

– unsigned: (216 - 1) * (216 - 1) = (232 - 2 * 216 + 1 < (232 - 1)

– signed: (-215) * (-215) = 230 < (231 - 1)

– overflow cannot occur

• Modification of Flags – Most flags are undefined after multiplication

– O and C flags clear to 0 if the result fit into half-size register

– e.g., if the most significant 16 bits of the product are 0, both flags C and O clear to 0

Page 4: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Multiplication (cont.)

• Two different instructions for multiplication– MUL Multiply unsigned

– IMUL Integer Multiply (2’s complement)

• Multiplication is performed on bytes, words, or double words• Which operation to perform depends on the size of the multiplier• The multiplier can be any register or any memory location

MUL CX ; AX * CX (unsigned result in DX--AX);IMUL WORD PTR [SI] ; AX * [word contents of memory location

; addressed by SI] (signed product in DX--AX)

Page 5: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Multiplication(16 bit)

The use of the AX (and DX) registers is implied!!!!!

Multiplicand AX

Multiplier (16-bit register,

16-bit memory variable)

DX, AX = PRODUCT

(High word in DX : Low word in AX)

Page 6: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Multiplication

• 8-bit multiplicationMultiplicand AL

Multiplier (8-bit register,

8-bit memory variable)

AX PRODUCT

• 32-bit multiplicationMultiplicand EAX

Multiplier (32-bit register,

32-bit memory variable)

EDX, EAX PRODUCT (High word in EDX : Low word in EAX)

– 32-bit multiplication is available only on 80386 and above

Page 7: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Binary Multiplication

• Long Multiplication is done through shifts and additions

• This works if both numbers are positive • To multiply a negative numbers, the CPU will store the sign bits

of the numbers, make both numbers positive, compute the result, then negate the result if necessary

0 1 1 0 0 0 1 0 (98) x 0 0 1 0 0 1 0 1 (37)

------------------------- 0 1 1 0 0 0 1 0 0 1 1 0 0 0 1 0 - - 0 1 1 0 0 0 1 0 - - - - - (3626)

Page 8: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Division

• X / Y = Q; R

X Dividend

Y Divisor

Q Quotient

R RemainderNote: Remainder has the same

sign as X (Dividend)

Examples (Signed Integers)

X / Y Q R

9 / 4 2 1-9 / 4 -2 -1 9 / -4 -2 1-9 / -4 2 -1

Page 9: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Division (cont.)

• Two different instructions for division– DIV Division unsigned

– IDIV Integer Division (2’s complement)

• Division is performed on bytes, words, or double words• Which operation to perform depends on the size of the divisor• The dividend is always a double-width dividend that is divided

by the operand (divisor)• The divisor can be any register or any memory location

Page 10: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Division(32-bit/16-bit)

The use of the AX (and DX) registers is implied!!!!!

Dividend DX, AX (high word in DX, low word in AX)

Divisor (16-bit register, 16-bit memory variable)

Quotient AX

Remainder DX

Page 11: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Division (cont.)

• 16-bit/8-bitDividend AX

Divisor (8-bit register, 8-bit memory variable)

Quotient AL

Remainder AH

• 64-bit/32-bitDividend EDX, EAX (high double word in EDX, low double word in EAX)

Divisor (32-bit register, 32-bit memory variable)

Quotient EAX

Remainder EDX• Available on 80386 and above

Page 12: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Division (cont.)

• Division of two equally sized words• Prepare the dividend

– Unsigned numbers: move zero into high order-word

– Signed numbers: use signed extension (implicitly uses AL, AX, DX registers) to fill high-word with ones or zeros

– CBW (convert byte to word)

AX = xxxx xxxx snnn nnnn (before)

AX = ssss ssss snnn nnnn (after)

– CWD (convert word to double)

DX:AX = xxxx xxxx xxxx xxxx snnn nnnn nnnn nnnn (before)

DX:AX = ssss ssss ssss ssss snnn nnnn nnnn nnnn (after)

– CWDE (convert double to double-word extended) - 80386 and above

Page 13: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Division (cont.)

• Flag settings– none of the flag bits change predictably for a division

• A division can result in two types of errors– divide by zero

– divide overflow (a small number divides into a large number), e.g., 3000 / 2

• AX = 3000;

• Devisor is 2 => 8 bit division is performed

• Quotient will be written to AL => but 1500 does not fit into AL

• consequently we have divide overflow• in both cases microprocessor generates interrupt (interrupts are

covered later in this course)

Page 14: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Division (Example)

Division of the byte contents of memory NUMB by the contents of NUMB1

Unsigned

MOV AL, NUMB ;get NUMBMOV AH, 0 ;zero extendDIV NUMB1MOV ANSQ, AL ;save quotientMOV ANSR, AH ;save remainder

Signed

MOV AL, NUMB ;get NUMBCBW ;signed-extendIDIV NUMB1MOV ANSQ, AL ;save quotientMOV ANSR, AH ;save remainder

Page 15: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Division (cont.)

• What do we do with remainder after division?– use the remainder to round the result

– drop the remainder to truncate the result

– if the division is unsigned, rounding requires that remainder is compared with half the divisor to decide whether to round up the quotient

– e.g., sequence of instructions that divide AX by BL and round the result

DIV BLADD AH, AH ;double remainderCMP AH, BL ;test for roundingJB NEXTINC AL

NEXT:

Page 16: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Program Segment Prefix (PSP)

• When a program is loaded into memory for execution, DOS first builds up a program segment prefix immediately before the program is loaded into memory.

• This PSP contains lots of information, some of it useful, most of it obsolete.

• Understanding the layout of the PSP is essential for programmers designing assembly language programs.

• The PSP is 256 bytes long

Page 17: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Program Segment Prefix (PSP)

Offset

0

2

4

5

0Ah

0Eh

12h

16h

2Ch

2Eh

50h

53h

5Ch

6Ch

80h

81h

Length

2

2

1

5

4

4

4

22

2

34

3

9

16

20

1

127

Description

An INT 20h instruction is stored here

Program ending address

Unused, reserved by DOS

Call to DOS function dispatcher

Address of program termination code

Address of break handler routine

Address of critical error handler routine

Reserved for use by DOS

Segment address of environment area

Reserved by DOS

INT 21h, RETF instructions

Reserved by DOS

Default FCB #1

Default FCB #2

Length of command line string

Command line string

Page 18: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

PSP – Program Ending Address

• Field number two contains a value which points to the last memory address allocated to your program.

• By subtracting the address of the PSP from this value, you can determine the amount of memory allocated to your program

Page 19: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

PSP – Environment Area Address

• This field contains the segment address of the environment storage area

• The environment strings always begin from an offset of zero from the above segment address

• This area of memory consists of a sequence of zero-terminated strings using the format:string1 0 string2 0 string3 0 0

• Strings are usually placed in the environment area using DOS commands like PATH or SET

• Generally an environment string takes the formname = parameters

Page 20: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

PSP – Environment Area Address

• For example the statementset ipath=c:\assembly\includecopies the string “ipath=c:\assembly\include” into the environment string storage area.

• Many programs scan the env storage area for paths and other information. Your programs can take advantage of this too.

Page 21: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

PSP – Command Line String

• Many programs allow you to append parameters after the executable name:e.g. Notepad mydoc.txt

• This command line string is stored in the PSP• Location 80h of the PSP stores the length of the CLS• Locations 81h through FFh contain the string itself• You can use CLS in your ASM programs just like you

would use argc, argv to access command line parameters in C/C++

Page 22: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

PSP – Command Line String

• For example, considerMYPGM parameter1, parameter2

• The CLS for this will be 23, “ parameter1, parameter2”, 0Dh

• Notice that the carriage return character is not figured into the length

• Please read Section 13.3.12 of the online text for an in depth discussion of this topic with parsing examples

Page 23: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Accessing the PSP

• Although the PSP is loaded into memory immediately before your program, that doesn’t necessarily mean that it appears 100h bytes before your code

• Your data segments may have been loaded into memory before your code segments, thereby invalidating this method of locating the PSP

• The segment address of the PSP is passed to your program in the DS register

• Use the following code to extract the PSP segment address

Page 24: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Accessing the PSP

Push ds ;Save PSP value in the stack

Mov ax, seg DSEG;Point DS and ES to our data segment

Mov ds, ax

Mov es, ax

Pop PSP ;Store PSP segment address into “PSP variable

Page 25: ECE291 Computer Engineering II Lecture 7

Z. Kalbarczyk ECE291

Accessing the PSP

• In DOS 5.0 and later, you can make a DOS call to obtain the PSP address

• Load AH with 51h and execute an int 21h instruction

• DOS will return the segment address of the current PSP in the bx register