26
ECE291 Computer Engineering II Lecture 6 Josh Potts University of Illinois at Urbana- Champaign

ECE291 Computer Engineering II Lecture 6

  • Upload
    rane

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

ECE291 Computer Engineering II Lecture 6. Josh Potts University of Illinois at Urbana- Champaign. Outline. Program organization NASM directives Multiplication Division Macros. Program Organization. - PowerPoint PPT Presentation

Citation preview

Page 1: ECE291 Computer Engineering II Lecture 6

ECE291Computer Engineering II

Lecture 6

Josh Potts

University of Illinois at Urbana- Champaign

Page 2: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 2 of 26

Outline

• Program organization

• NASM directives

• Multiplication

• Division

• Macros

Page 3: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 3 of 26

Program Organization

• Create block structure and/or pseudocode on paper to get a clear concept of program control flow and data structures

• Break the total program into logical procedures/macros

• Use jumps, loops, etc. where appropriate

• Use descriptive names for variables

– noun_type for types

– nouns for variables

– verbs for procedures/functions

Page 4: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 4 of 26

Debugging Hints

• Good program organization helps

• Programs do not work the first time

• Strategy to find problems

– Use DEBUG breakpoints to check program progress

– Use COMMENT to temporarily remove sections of code

– "print" statements announce milestones in program

• Test values/cases

– Try forcing registers/variables to test output of a procedure

– Use "print" statements to display critical data

• Double-check your own logic (Did you miss a special case?)

• Try a different algorithm, if all else fails...

Page 5: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 5 of 26

NASM Directives

• Includes %include “drive:\path\filename”

• Definitions

– DB/RESB define/reserve byte (8bits)

– DW/RESW define/reserve word (16 bits)

– DD/RESD define/reserve double word (32 bits)

– EQU names a constant

• Labels

– “.” prefixed “local” to previous non-dotted label

– “:” suffix (optional, not required)

Page 6: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 6 of 26

NASM Directives(cont.)

• Macros

– Instead of using procedures, which require both stack and time resources, macros are fast and flexible

– Advantages:

• speed; no call instruction

• readability - easier to understand program function

– Drawbacks -

• space using the macro multiple times duplicates the code

• tricky to debug (in particular when you have nested macros)

• Procedures

– “name”

– use “.” (local) labels inside procedure (helps keep labels unique)

Page 7: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 7 of 26

NASM Directives (cont.)

• References to procedures

– EXTERN “name” – give you access to procedures/variables in other files

– GLOBAL “name” – makes your procedures/variables available to other files

• Segment definition

– SEGMENT “name” [STACK]

Page 8: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 8 of 26

Example Program Structure

; ECE291:MPXXX

; In this MP you will develop program which take input

; from the keyboard…………

;====== Constants=================================================

;ASCII values for common characters

CR EQU 13

LF EQU 10

ESCKEY EQU 27

;====== Externals =================================================

; -- LIB291 Routines

extern dspmsg, dspout, kbdin

extern rsave, rrest, binasc

Page 9: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 9 of 26

Example Program Structure (cont.);==== LIBMPXXX Routines (Your code will replace calls to these

functions)

extern LibKbdHandler

extern LibMouseHandler

extern LibDisplayResult

extern MPXXXXIT

;====== Stack =====================================================

stkseg segment stack ; *** STACK SEGMENT ***

resb 64*8 ; 64*8 = 512 Bytes of Stack

stacktop:

;====== Begin Code/Data ============================================

segment CODE ; *** CODE SEGMENT ***

Page 10: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 10 of 26

Example Program Structure (cont.)

;====== Variables =================================================

inputValid db 0 ; 0: InputBuffer is not ready

; 1: InputBuffer is ready

;-1: Esc key pressed

operandsStr db 'Operands: ','$'

OutputBuffer 16 times db 0 ; Contains formatted output

db ‘$’ ; (Should be terminated with '$')

MAXBUFLENGTH EQU 24

InputBuffer MAXBUFLENGTH times db 0 ; Contains one line of db ‘$’ ; user input

graphData %include “graphData.dat” ; data

GLOBAL OutputBuffer, inputValid, operandsStr

GLOBAL graphData

Page 11: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 11 of 26

Example Program Structure (cont.)

;====== Procedures ===========================================

KbdHandler

<Your code here>

MouseHandler

<Your code here>

DisplayResult

<Your code here>

;====== Program Initialization ===============================

..start:

mov ax, cs ; Use common code & data segment

mov ds, ax

mov ax, stkseg ; Initialize stack

mov ss, ax

mov sp, stacktop

Page 12: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 12 of 26

Example Program Structure (cont.)

;====== Main Procedure ========================================

MAIN:

MOV AX, 0B800h ;Use extra segment to access video screen

MOV ES, AX

<here comes your main procedure>

CALL MPXXXXIT ; Exit to DOS

Page 13: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 13 of 26

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 14: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 14 of 26

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 [si] ; AX * [word content of memory location

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

Page 15: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 15 of 26

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 16: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 16 of 26

Multiplication (cont.)

• 8086/8088 microprocessors do not allow to perform immediate multiplication

• 80286, 80386, and 80486 allow the immediate multiplication by using a special version of the multiply instruction

• Immediate multiplication must be signed multiplication and contains three operands

– 16-bit destination register

– register or memory location that contains 16-bit multiplicand

– 8-bit or 16-bit immediate data used as a multiplier

imul cx, dx, 12h ;multiplies 12h * DX and leaves ;16-bit signed product

in CX

Page 17: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 17 of 26

Multiplication

• 8-bit multiplication

Multiplicand AL

Multiplier (8-bit register,

8-bit memory variable)

AX PRODUCT

• 32-bit multiplication

Multiplicand 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 18: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 18 of 26

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 19: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 19 of 26

Division

• X / Y = Q; R

X Dividend

Y Divisor

Q Quotient

R Remainder

Note: 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 20: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 20 of 26

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 21: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 21 of 26

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 22: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 22 of 26

Division (cont.)

• 16-bit/8-bit

Dividend AX

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

Quotient AL

Remainder AH

• 64-bit/32-bit

Dividend 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 23: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 23 of 26

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 24: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 24 of 26

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 25: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 25 of 26

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 byte [NUMB1]MOV [ANSQ], AL ;save quotientMOV [ANSR], AH ;save remainder

Signed

MOV AL, [NUMB] ;get NUMBCBW ;signed-extendIDIV byte [NUMB1]MOV [ANSQ], AL ;save quotientMOV [ANSR], AH ;save remainder

Page 26: ECE291 Computer Engineering II Lecture 6

31 January 2001 ECE291 Lecture 6 Slide 26 of 26

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: