CSC 221 Computer Organization and Assembly Language

Slides:



Advertisements
Similar presentations
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Advertisements

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Computer Organization And Assembly Language
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You.
CS2422 Assembly Language & System Programming October 17, 2006.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Flow Control Instructions
Conditional Processing If … then … else While … do; Repeat … until.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Web siteWeb site ExamplesExamples ASSEMBLY LANGUAGE FOR INTEL- BASED COMPUTERS, 5 TH EDITION Chapter 6: Conditional Processing Kip R. Irvine.
Wednesday Feb 1 Project #1 Due Sunday, February 3 Quiz #2 Wednesday, February 6, in class Programming Project #2 is posted Due Sunday, February 10.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You.
Assembly Language for x86 Processors 6th Edition
Chapter 6: Conditional Processing. 2 Chapter Overview Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions Conditional.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
26-Nov-15 (1) CSC Computer Organization Lecture 6: Pentium IA-32.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 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.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
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.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
CS2422 Assembly Language and System Programming 0 Week 13 & 14 Codes in Assembly Language.
CS-401 Computer Architecture & Assembly Language Programming
Practical Session 2 Computer Architecture and Assembly Language.
Computer Architecture and Assembly Language
Assembly Language for x86 Processors 6th Edition
Assembly Language for x86 Processors 7th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Chapter 6: Conditional Processing
Computer Architecture and Assembly Language
Practical Session 2.
CSC 221 Computer Organization and Assembly Language
Chapter 9.
Assembly Language for x86 Processors 7th Edition
Assembly Language for Intel-Based Computers, 4th Edition
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
Assembly IA-32.
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
Processor Processor characterized by register set (state variables)
Computer Organization and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for Intel-Based Computers, 5th Edition
X86 Assembly Review.
Conditional Processing
Assembly Language for Intel 8086 Jump Condition
Computer Architecture and Assembly Language
Presentation transcript:

CSC 221 Computer Organization and Assembly Language Lecture 18: Program Components, I/O and Conditional Jump Instructions

Lecture 17: Review AND Instruction OR Instruction XOR Instruction AND destination, source OR Instruction OR destination, source XOR Instruction XOR destination, source NOT Instruction NOT destination

Lecture 17: Review (cont.) Applications TEST Instruction Performs a nondestructive AND operation between each pair of matching bits in two operands. ZERO Flag CMP Instruction CMP destination, source Zero (Dest. Equal) and Carry (Dest. Less) ZF = CF = 0 (Dest. > Src)

Lecture Outline Program Components I/O Instructions Jumps Based On . . . Specific flags Equality Unsigned comparisons Signed Comparisons Applications

Program Components .386 Assembler directive tells the assembler to use the 386 instruction set. There are hardly any processors out there that are older than the 386 nowadays. Alternatively, you can use .486, .586 or .686, but .386 will be the most compatible instruction set.

Program Components (cont.) .model flat, stdcall .MODEL memorymodel [, langtype] [, stackoption] memorymodel Required parameter that determines the size of code and data pointers. langtype Optional parameter that sets the calling and naming conventions for procedures and public symbols. stackoption Optional parameter. stackoption is not used if memory model is FLAT. NEARSTACK groups the stack segment into a single physical segment along with data (DS=SS)

Program Components (cont.) Parameter 32-bit values 16-bit values (support for earlier 16-bit development) memorymodel FLAT TINY , SMALL, COMPACT, MEDIUM, LARGE, HUGE, FLAT langtype C , STDCALL C , BASIC, FORTRAN, PASCAL, SYSCALL, STDCALL stackoption Not used NEARSTACK , FARSTACK

Program Components (cont.) .model flat, stdcall MODEL is an assembler directive that specifies the memory model of your program. flat is the model for Windows programs. stdcall is the parameter passing method used by Windows functions, which means you need to push your parameters from right-to-left. NOTE: The flat memory model uses 32-bit segments and must be preceded by a .386 or .486 directive.

Program Components (cont.) option casemap :none Forces your labels to be case sensitive, which means Hello and hello are treated differently. This is a good habit to learn because, Most high level programming languages are also case sensitive.

Program Components (cont.) include \masm32\include\windows.inc Include files required for Windows programs. include \masm32\include\windows.inc It is always included, since it contains the declarations for the Win32 API constants and definitions. include \masm32\include\kernel32.inc Contains the ExitProcess function that we use.

Libraries includelib \masm32\lib\kernel32.lib includelib \masm32\lib\masm32.lib Functions need libraries in order to function

I/O Instructions StdIn proc lpszBuffer:DWORD,bLen:DWORD Description StdIn receives text input from the console and places it in the buffer required as a parameter. The function terminates when Enter is pressed. Parameters 1. lpszBuffer The buffer to receive the text input from the console. 2. bLen The length of the buffer. Return Value There is no return value. Include \masm32\include\masm32.inc includelib \masm32\lib\masm32.lib invoke StdIn, addr buffer, 100 Source: C:\masm32\help\masmlib.chm

I/O Instructions (cont.) StdOut proc lpszText:DWORD Description: StdOut will display a zero terminated string at the current position in the console. Parameter lpszText : A zero terminates string. Return Value There is no return value. Include \masm32\include\masm32.inc includelib \masm32\lib\masm32.lib invoke StdOut, addr message1 Source: C:\masm32\help\masmlib.chm

I/O Instructions: Example .data message1 BYTE "Type your name: ", 0 message2 BYTE "Your name is ", 0 .data? buffer BYTE 100 dup(?) .code start: invoke StdOut, addr message1 invoke StdIn, addr buffer, 100 invoke StdOut, addr message2 invoke StdOut, addr buffer invoke ExitProcess, 0 end start

Conditional Jump (Jcond) Instructions A conditional jump instruction branches to a label when specific register or flag conditions are met. Examples: JB, JC jump to a label if the Carry flag is set JE, JZ jump to a label if the Zero flag is set JS jumps to a label if the Sign flag is set JNE, JNZ jump to a label if the Zero flag is clear JECXZ jumps to a label if ECX equals 0

Jcond Ranges Prior to the 386: IA-32 processors: jump must be within –128 to +127 bytes from current location counter. IA-32 processors: 32-bit offset permits jump anywhere in memory.

Jumps Based on Specific Flags

Jumps Based on Equality

Jumps Based on Unsigned Comparisons

Jumps Based on Signed Comparisons

Applications (1 of 5) cmp eax,ebx ja Larger Task: Jump to a label if unsigned EAX is greater than EBX Solution: Use CMP, followed by JA cmp eax,ebx jg Greater Task: Jump to a label if signed EAX is greater than EBX Solution: Use CMP, followed by JG

Applications (2 of 5) cmp eax,Val1 jbe L1 ; below or equal Jump to label L1 if unsigned EAX is less than or equal to Val1 cmp eax,Val1 jle L1 Jump to label L1 if signed EAX is less than or equal to Val1

Applications (3 of 5) mov Large,bx cmp ax,bx jna Next mov Large,ax Next: Compare unsigned AX to BX, and copy the larger of the two into a variable named Large mov Small,ax cmp bx,ax jnl Next mov Small,bx Next: Compare signed AX to BX, and copy the smaller of the two into a variable named Small

Applications (4 of 5) Jump to label L1 if the memory word pointed to by ESI equals Zero cmp WORD PTR [esi],0 je L1 Jump to label L2 if the doubleword in memory pointed to by EDI is even test DWORD PTR [edi],1 jz L2

Applications (5 of 5) Task: Jump to label L1 if bits 0, 1, and 3 in AL are all set. Solution: Clear all bits except bits 0, 1,and 3. Then compare the result with 00001011 binary. and al,00001011b ; clear unwanted bits cmp al,00001011b ; check remaining bits je L1 ; all set? jump to L1

Summary Program Components I/O Instructions Processor .MODEL etc. StdIn invoke StdIn, addr buffer, 100 StdOut invoke StdOut, addr message1

Summary Conditional Jump Instructions Jumps Based on Specific Flags JZ, JNZ, JC, JNC, …… Jumps Based on Equality JE, JNE, JCXZ, ….. Jumps Based on Unsigned Comparison JA, JB, JAE, JBE, ….. Jumps Based on Signed Comparison JG, JL, JGE, JLE, ….

Reference Most of the Slides are taken from Presentation: Chapter 6 Assembly Language for Intel-Based Computers, 4th Edition Kip R. Irvine (c) Pearson Education, 2002. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.