K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung

Slides:



Advertisements
Similar presentations
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
Advertisements

Department of Computer Science and Software Engineering
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
80x86 Instruction Set Dr. Qiang Lin.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
Flow Control Instructions
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Shift and Rotate Instructions
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Conditional Processing If … then … else While … do; Repeat … until.
Ch. 7 Logic, Shift and Rotate instr.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Lecture 5 Multiplication, Division and Branches Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Multiplication and Division Instructions & the 0Ah function.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Logical and Bit Operations Chapter 9 S. Dandamudi.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Comparison Instructions Test instruction –Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Multiplication and Division instructions Dr.Hadi AL Saadi.
Practical Session 2 Computer Architecture and Assembly Language.
Chapter Nov-2010
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Practical Session 2.
Microprocessor Systems Design I
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Computer Architecture
Basic Assembly Language
Multiplication and Division Instructions
Assembly IA-32.
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
# include < stdio.h > v oid main(void) { long NUM1[5]; long SUM; long N; NUM1[0] = 17; NUM1[1] = 3; NUM1[2] =  51; NUM1[3] = 242; NUM1[4] = 113; SUM =
Processor Processor characterized by register set (state variables)
4.2 Arithmetic Instructions
CS 301 Fall 2002 Assembly Instructions
Chapter 4: Instructions
فصل پنجم انشعاب و حلقه.
Shift & Rotate Instructions)
Homework Reading Machine Projects Labs PAL, pp
Multiplication and Division Instructions
Shift & Rotate Instructions)
Flow Control Instructions
X86 Assembly Review.
EECE.3170 Microprocessor Systems Design I
Multiplication and Division Instructions
Multiplication and Division Instructions
Carnegie Mellon Ithaca College
Chapter 8: Instruction Set 8086 CPU Architecture
Division instruction.
Computer Architecture and Assembly Language
Presentation transcript:

K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung Heavily based on materials by Naranker Dulay

K.K. Leung Fall 2008Introductory Pentium Programming2 Topics Expressions Overflow and Divide by Zero Booleans & Comparison If Statements & Loops Methods Parameter Passing Local Variables

K.K. Leung Fall 2008Introductory Pentium Programming3 Integer Addition & Subtraction InstructionOperationNotes add dst, srcdst = dst + srcadd subdst, srcdst = dst - srcsubtract cmpdst, srcdst - srccompare & set flag bits incopropr = opr + 1increment by 1 decopropr = opr - 1decrement by 1 negopropr = - oprnegate Operands can be byte, word or doubleword sized Arithmetic instructions also set flag bits, e.g. the zero flag (zf), the sign flag (sf), the carry clag (cf), the overflow flag (of). Flags are used by branching instructions.

K.K. Leung Fall 2008Introductory Pentium Programming4 Integer Multiply InstructionOperation imul destreg, srcoprdestreg = destreg * srcopr imul destreg, srcreg, immediatedestreg = srcreg * immediate imul destreg, memopr, immediatedestreg = memopr * immediate Operands can be word or doubleword sized

K.K. Leung Fall 2008Introductory Pentium Programming5 Integer Divide InstructionOperation Notes idiv opral = ax / opr Word/Byte ah = ax mod opr ax = (dx:ax) / opr Doubleword/Word dx = (dx:ax) mod opr eax = (edx:eax) / opr Quadword/ Dword edx = (edx:eax) mod opr Operands must be registers or memory operands

K.K. Leung Fall 2008Introductory Pentium Programming6 More Instructions InstructionOperationNotes sal dst, ndst = dst * 2 n shift arithmetic left sar dst, ndst = dst / 2 n shift arithmetic right sal and sar are quick ways of multiplying/dividing by powers of 2 where n must be a constant (immediate value) or the byte register cl. cbw ax = al convert byte to word cwdeeax = ax convert word to doubleword cdqedx:eax = eax convert double to quadword cbw, cwde & cdq extend a signed integer by filling the extra bits of destination with the sign bit of the operand (i.e. preserve value of result)

K.K. Leung Fall 2008Introductory Pentium Programming7 Expressions intalpha=7, beta=4, gamma=-3 /* global variables */ alpha = (alpha * beta + 5 * gamma) * (alpha – beta)... In this example we’ll represent integers as 16-bit 2’s complement values and use global variable and direct addressing. alphadw7 beta dw4 gammadw-3

K.K. Leung Fall 2008Introductory Pentium Programming8 Example ; int alpha = 7; beta = 4; gamma = -3 ; alpha = (alpha * beta + 5 * gamma) * (alpha – beta) 001C imul 001C 0005 mov 001C FFF1 imul 000D FFF1 add 0007 ax bx cx dx mov regs shown in hex mov ax, [alpha]; ax = alpha imul ax, [beta]; ax = alpha * beta mov bx, 5; bx = 5 imul bx, [gamma]; bx = 5 * gamma add ax, bx; ax = 5 * gamma + alpha * beta

K.K. Leung Fall 2008Introductory Pentium Programming9 Example Continued ; alpha = 7; beta = 4; gamma = -3 ; alpha = (alpha * beta + 5 * gamma) * (alpha – beta) 000D FFF1 ax bx cx dx prev 000D 0007 mov 000D 0003 sub imul 0027 alpha mov sub bx, [beta]; bx = alpha – beta mov bx, [alpha]; bx = alpha imul ax, bx; ax = ax * (alpha-beta) mov [alpha], ax; alpha := final value

K.K. Leung Fall 2008Introductory Pentium Programming10 Integer Overflow Most arithmetic operations can produce an overflow, for example for signed byte addition if A + B > 127 or A + B < -128 Instructions which result in an overflow set the overflow flag in the eflags register, which we can test, e.g. Overflow Test add ah, bh; add, will set FLAGS.OF on overflow jo ov_label; jump to ov_label if overflow... ov_label:; handle overflow condition somehow?

K.K. Leung Fall 2008Introductory Pentium Programming11 Integer Divide by Zero Another erroneous condition is division by zero which causes an interrupt to occur (we will cover interrupts later in the course). We can guard against this occurring by explicitly checking the divisor before division, e.g. Divide by Zero Test cmpbh, 0; compare divisor with zero jezero_div; jump if (divisor) is equal to zero idivbh; else perform division zero_div:....; handle divide by zero somehow?

K.K. Leung Fall 2008Introductory Pentium Programming12 “LOGICAL” (Bit-level) Instructions InstructionOperationNotes and dst, srcdst = dst & srcbitwise and testdst, srcdst & srcbitwise and, also set flags ordst, srcdst = dst | srcbitwise or xordst, srcdst = dst ^ srcbitwise xor notopropr = ~ oprbitwise not Typical Uses and is used to clear specific bits (the 0 bits in src) in dst. or is used to set specific bits (the 1 bits in src) in dst. xor is used to toggle/invert specific bits (the 1 bits in src) in dst. test is used to test specific bit patterns.

K.K. Leung Fall 2008Introductory Pentium Programming13 Booleans We’ll use a full byte to represent a boolean value with the following interpretation: False = 0, True = Non-Zero boolean man, rich, okay... ; okay = (man && rich) || ( ! man ) mov al, [man]; al = man and al, [rich]; al = man && rich mov ah, [man]; ah = man not ah; ah = not man or al, ah; al = (man && rich) || ! man mov [okay], al; okay = al

K.K. Leung Fall 2008Introductory Pentium Programming14 JUMP Instructions Jump instructions take the form OPCODE label, e.g. JGE next OpcodeFlag ConditionsNotes jmp unconditionaljump je or jz zf = 1jump if equal or jump if zero (==) jne or jnzzf = 0jump if not equal or jump if not zero Signed Comparisons jg zf = 0 and sf = 0jump if greater than ( > ) jge sf = 0jump if greater than or equal ( >= ) jl sf = 1jump if less than ( < ) jle zf = 1 or sf = 1jump if less than or equal ( <= )

K.K. Leung Fall 2008Introductory Pentium Programming15 More JUMP Instructions Unsigned Comparisons ja zf = 0 and cf = 0jump if above ( > ) jaecf = 0jump if above or equal ( >= ) jb cf = 1jump if below ( < ) jbe zf = 1 or cf = 1 jump if below or equal ( <= ) Miscellaneous jo of = 1jump if overflow jnoof = 0 jump if no overflow, ditto for...

K.K. Leung Fall 2008Introductory Pentium Programming16 If Statement if (age < 100) { statements } if: cmp word[age],100 jl stats jmp endif stats: ; statements endif: if: cmp word[age],100 jge endif ; statements endif:

K.K. Leung Fall 2008Introductory Pentium Programming17 if: cmpword[age], 21 jlendif cmpword[age], 65 jgendif ; statements endif: If Statement Contd if (age >= 21) && (age <= 65) { statements }

K.K. Leung Fall 2008Introductory Pentium Programming18 IF-Then-Else Statement If (age < 100) { statements1 } else { statements2 } if: cmpword[age], 100 jgeelse ; statements1 jmpendif else: ; statements2 endif:

K.K. Leung Fall 2008Introductory Pentium Programming19 While Loop while (age <= 99) { statements } while: cmpword[age], 99 jgendwhile ; statements jmpwhile endwhile:

K.K. Leung Fall 2008Introductory Pentium Programming20 do { statements } while (age <= 99) while: ; statements cmpword[age], 99 jlewhile endwhile: Do-While Loop

K.K. Leung Fall 2008Introductory Pentium Programming21 for (age = 1; age<=99; age++) { statements } for:mov[word[age], 1 next: cmp[word[age], 99 jgendfor ; statements incword[age] jmpnext endfor: For Loop