Thursday, January 21 Program #1 is posted Due Friday, June 9th Quiz #2 Thursday, July 15th.

Slides:



Advertisements
Similar presentations
Introduction to Assembly Language
Advertisements

Chapter 7 Introduction to LC-3 Assembly Language Assembly Language Assembly Process Using the Editor & Simulator for Assembly Language Programming.
MSP430 Teaching Materials
INSTRUCTION SET ARCHITECTURES
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Assembly Programming Notes for Practical2 Munaf Sheikh
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
Runtime Stack Managed by the CPU, using two registers
Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Intrinsic Data Types (1 of 2) BYTE, SBYTE 8-bit unsigned.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Assembly Language for Intel-Based Computers Chapter 3: Assembly Language Fundamentals Kip Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CS2422 Assembly Language & System Programming October 24, 2006.
Coding.
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Introduction to C Programming
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
CS2422 Assembly Language & System Programming September 26, 2006.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Joseph L. Lindo Assembly Programming Sir Joseph Lindo University of the Cordilleras.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Chapter 2 Software Tools and Assembly Language Syntax.
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
1/2002JNM1 Basic Elements of Assembly Language Integer Constants –If no radix is given, the integer is assumed to be decimal. Int 21h  Int 21 –A hexadecimal.
Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You may modify.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
CSC 221 Computer Organization and Assembly Language
ASSEMBLY LANGUAGE FOR INTEL-BASED COMPUTERS, PROCEDURES.
Sahar Mosleh California State University San MarcosPage 1 Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
In Class Program Write, assemble and test a program: –Use the DB directive to define the following list of numbers and name it array: 31h, 32h, 33h, 34h.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Computer Science 210 Computer Organization
Format of Assembly language
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Microprocessor and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Chapter 4: Instructions
BIC 10503: COMPUTER ARCHITECTURE
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Computer Science 210 Computer Organization
Introduction to C Programming
Computer Organization and Assembly Language
Hardware & Software Architecture
Computer Architecture and System Programming Laboratory
Chapter 10 Instruction Sets: Characteristics and Functions
BASIC SYNTAX OF ASSEMBLY LANGUAGE “HELLO WORLD” PROGRAM.
Presentation transcript:

Thursday, January 21 Program #1 is posted Due Friday, June 9th Quiz #2 Thursday, July 15th

Today’s topics Assembly language program development Introduction to MASM

Assembler, Linker, IDE Install Visual C Express Edition (if you don’t already have a version of Visual C++) Install Visual C Express Edition (if you don’t already have a version of Visual C++) Install the Microsoft Assembler Install the Microsoft Assembler Install the libraries Install the libraries We will use Irvine's library (for now) to handle the really awful stuff. We will use Irvine's library (for now) to handle the really awful stuff. input / output, screen control, timing, etc. input / output, screen control, timing, etc.

Additional resources Course website “Resources” Course website “Resources” MASM Guide MASM Guide MASM instruction set MASM instruction set Template for all programs Template for all programs Demo programs Demo programs Etc. Etc.

Program development Design Implement Test / Debug Use and maintain

Development tools Editor Assembler Libraries Linker Loader Operating system

Program design and algorithms Text editor Text source code (.asm file) Assembler Library files (.inc,.lib) Linker Binary Executable code (.exe file) Binary program in memory Binary machine code (.obj file) Instruction Execution Cycle (Operating System) execution begins Loader

MASM instruction types Move data Move data Arithmetic Arithmetic Compare two values Compare two values Conditional/unconditional branch Conditional/unconditional branch Call procedure Call procedure Loop control Loop control I/O I/O

Instruction formats Variable length Variable length Opcode Opcode Fixed length Fixed length Operand specification Operand specification different “addressing modes” for different opcodes different “addressing modes” for different opcodes different number of operands for different opcodes different number of operands for different opcodes opcode opcode opcodedestination opcodedestination opcodedestination, source opcodedestination, source

Addressing modes Immediate Set register to a constant Immediate Set register to a constant Direct Set register to address of global Direct Set register to address of global Register Use register as operand Register Use register as operand Register indirect Access memory through address in a register Register indirect Access memory through address in a register Indexed “array” element, using offset in register Indexed “array” element, using offset in register Base-indexed Start address in one register; offset in another, add and access memory Base-indexed Start address in one register; offset in another, add and access memory Stack Memory area specified and maintained as a stack; stack pointer in register Stack Memory area specified and maintained as a stack; stack pointer in register Offset (branch) “ goto” address; may be computed Offset (branch) “ goto” address; may be computed

MASM data types TypeUsed for: BYTE Strings, 1-byte unsigned integers [0 … 255] SBYTE 1-byte signed integers [-128 … 127] WORD 2-byte unsigned integers [0 … 65535], address SWORD 2-byte signed integers [ … 32767] DWORD 4-byte unsigned integers [0 … ], address SDWORD 4-byte signed integers [ … ] FWORD 6-byte integer QWORD 8-byte integer TBYTE 10-byte integer REAL4 4-byte floating-point REAL8 8-byte floating-point REAL10 10-byte floating-point

Memory locations May be named May be named Name can refer to a variable name or a program label Name can refer to a variable name or a program label Interpretation of contents depends on program instructions Interpretation of contents depends on program instructions Numeric data Numeric data Integer, floating point Integer, floating point Non-numeric data Non-numeric data Character, string Character, string Instruction Instruction Address Address etc. etc.

General form of a MASM statement Comments start with ; Comments start with ; Segments start with. Segments start with. Each instruction line has 4 fields: Each instruction line has 4 fields: Label Label Opcode Opcode Operands Operands Comment Comment Depending on the opcode, one or more operands may be required Depending on the opcode, one or more operands may be required Otherwise, any field may be empty Otherwise, any field may be empty If empty opcode field, operand field must be empty If empty opcode field, operand field must be empty

TITLE Program Template (template.asm) ; Author: ; Course/project IDDate: ; Description: ; (include any libraries here) ; (insert symbol definitions here).data ; (insert variables here).code main PROC ; (insert executable instructions here) exit; exit to operating system main ENDP ; (insert additional procedures here) END main

Getting started We will use Irvine's library (for now) to handle the really awful stuff. input / output screen control timing etc. Check the Resources page, MASM Example program development walk-through

MASM program TITLE directive you can put anything you want … but the grader wants to see a meaningful title and the name of the source code file ; identification block technically optional (as are all comments) … but the grader wants to see information (see template.asm ) INCLUDE directive copies a file of definitions and procedures into the source code use Irvine32.inc for now

MASM program Global constants may be defined.data directive marks beginning of data segment variable declarations go here.code directive marks end of data segment and beginning of code segment main procedure defined here (required) other procedures defined here (optional)

Data definition in the.data segment in the.data segment General form is General form is labeldata_type initializer(s);comment label is the "variable name" label is the "variable name" data_type is one of (see previous slide) data_type is one of (see previous slide) at least one initializer is required at least one initializer is required may be ?(value to be assigned later) may be ?(value to be assigned later) Examples: Examples: sizeDWORD100;class size temperatureSWORD-10;current Celsius responseBYTE'Y';positive answer gpaREAL4?;my GPA myNameBYTE ” Wile E. Coyote ”,0

main procedure in the.code segment General form is mainPROC ; ( program instructions ) mainENDP ; ( other procedures ) ENDmain

Identifiers 1 to 247 characters (no spaces) NOT case sensitive! Start with letter, or $ For now, don’t use or $ Remaining characters are letters, digits, or _ Identify variables, constants, procedures, and labels Cannot be a reserved word

Literals Actual values, named constants Assign contents of registers, memory Initialize variables in the.data segment Integer Floating point Character String

Literals Integer Optional radix: b, q/o, d, h Digits must be consistent with radix Hex values that start with a letter must have leading 0 Default (no radix) is decimal Floating-point (real) Optional sign Standard notation(e.g., ) Exponent notation(e.g.,-3.5E26.15E-3) Must have decimal point

Literals Character Single character in quotes ’ a ’” * ” Single quotes recommended String Characters in quotes ’ always ’,0 ” 123 * 654 ”,0 Double quotes recommended Embedded quotes must be different ” It ’ s ”,0 ’ Title: ” MASM ”’,0 Strings must be null-terminated Always end with zero-byte

Directives Tell the assembler how to interpret the code Mark beginning of program segments.data.code Mark special labels mainproc

Instructions For now, know how to use mov add sub mul, imul div, idiv inc dec loop Some instructions use implied operands See Irvine (Appendix B) or on-line Instructions

Easy Instructions movop1, op2;op2 is copied to op1 addop1, op2;op2 is added to op1 subop1, op2;op2 is subtracted from op1 incop; add 1 to op decop; subtract 1 from op For 2-operand instructions the first operand is the destination and the second operand is the source 2-operand instructions require at least one of the operands to be a register (or op2 must be literal). Note: op1 can not be a literal ! (Why?)

Instructions with implied operands mul, imulimplied operand must be in eax mulop2; result is in EDX:EAX Example: moveax,10 movebx,0Ch mulebx; result is in eax (120), ; with possible ; overflow in edx

Instructions with implied operands div, idivimplied operand is in EDX:EAX so set edx to 0 before division divop2; quotient is in EAX ; remainder is in EDX Example: moveax,100 movedx,0 movebx,9 divebx; quotient is in eax (11) ; remainder is in edx (1)

Instructions with implied operands loopimplied operand is ecx so set ecx to the loop count, and put a label at the beginning of the loop movecx,10 repeat: ; loop body ;… looprepeat ecx is automatically decremented by 1 and tested each time the loop statement is executed. When ecx becomes 0, the loop terminates.

Library Procedures - Overview p1 See IrvineLibHelp.exe at Clrscr : Clear the screen Preconditions: none Postconditions: screen cleared and cursor is at upper left corner Crlf : New line Preconditions: none Postconditions: cursor is at beginning of next new line

Library Procedures - Overview p2 ReadInt : Reads a 32-bit signed decimal integer from keyboard, terminated by the Enter key. Preconditions: none Postconditions: value entered is in EAX ReadString : Reads a string from keyboard, terminated by the Enter key. Preconditions: OFFSET of memory destination in EDX Size of memory destination in ECX Postconditions: String entered is in memory Length of string entered is in EAX

Library Procedures - Overview p3 WriteDec : Writes an unsigned 32-bit integer to the screen in decimal format. Preconditions: value in EAX Postconditions: value displayed WriteInt - Writes a signed 32-bit integer to the screen in decimal format. Preconditions: value in EAX Postconditions: value displayed WriteString - Writes a null-terminated string to the screen. Preconditions: OFFSET of memory location in EDX Postconditions: string displayed

Calling a Library Procedure INCLUDE Irvine32.inc... mov eax,1234; input argument call WriteDec; show number call Crlf; end of line Call a library procedure using the CALL instruction. Some procedures require input arguments. The INCLUDE directive copies the procedure prototypes (declarations) into the program source code. Example: display "1234" on the console:

Calling a Library Procedure INCLUDE Irvine32.inc... mov saveA,eax;save the eax register mov eax,-123;value to display call WriteInt;show number call Crlf;end of line mov eax,saveA;restore eax Sometimes certain registers are used to pass parameters Sometimes values of registers must be saved (in memory) before calling a procedure, and restored to the original values when control returns.

In-line Comments Start with ; May be on separate line or at end of a line Use comments to clarify lines or sections Preferred … ; Calculate the number of students in class today. moveax,size subeax,absent movpresent,eax OK … moveax,size;start with class size subeax,absent;subtract absentees movpresent,eax;number present Terrible … moveax,size;move size into eax subeax,absent;subtract absent from eax movpresent,eax;move eax to present

Program Design Decide what the program should do Decide what the program should do Define algorithm(s) Define algorithm(s) Decide what the output should show Decide what the output should show Determine what variables/constants are required Determine what variables/constants are required

Implementing a MASM program Open project Open project Start with template Start with template Save as program name (.asm) Save as program name (.asm) Fill in header information Fill in header information Define constants Define constants Test*/fix (syntax check, nothing happens) Test*/fix (syntax check, nothing happens) Declare variables (.data section) Declare variables (.data section) Test*/fix (syntax check, nothing happens) Test*/fix (syntax check, nothing happens) Enter the output code Enter the output code Test*/fix (no calculations, usually everything shows 0) Test*/fix (no calculations, usually everything shows 0) Enter the input code Enter the input code Test*/fix (no calculations, echo input) Test*/fix (no calculations, echo input) Enter the calculation code Enter the calculation code Test*/fix (logic check, verify) Test*/fix (logic check, verify) * First try Debug, Start Without Debugging

Questions? Program #1 due Friday, July 9th, before midnight Quiz #2 Thursday, July 15th Read Irvine Chapter 4