CS2422 Assembly Language and System Programming 0 Week 7 & 8 Intro. To Assembly Programming.

Slides:



Advertisements
Similar presentations
Introduction to Assembly Language
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembly Language for x86 Processors 6th Edition
Assembly Language for Intel-Based Computers, 5th Edition
CS2422 Assembly Language and System Programming Assembly Language Fundamentals Department of Computer Science National Tsing Hua University.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
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 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
1 Lecture 3: Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
Assembly Language for Intel-Based Computers Chapter 3: Assembly Language Fundamentals Kip Irvine.
Introduction to Assembly Language
CS2422 Assembly Language & System Programming October 24, 2006.
Coding.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
CS2422 Assembly Language & System Programming September 26, 2006.
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 ;
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Chapter 3: Assembly Language Fundamentals
Assembly Fundamentals Computer Organization and Assembly Languages Yung-Yu Chuang 2005/10/13 with slides by Kip Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals Assembling, Linking and Running Programs Example Programs.
Assembly Language for x86 Processors 7th Edition
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.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
Machine Instruction Characteristics
Introduction to 8086 Assembly Language Assembly Language Programming University of Akron Dr. Tim Margush.
Faculty of Engineering, Electrical Department,
Summer 2014 Chapter 3: Assembly Language Fundamentals.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Chapter Five–80x86 Assembly program development Principles of Microcomputers 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 1.
Sahar Mosleh California State University San MarcosPage 1 Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello.
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.
Review of Assembly language. Recalling main concepts.
What is a program? A sequence of steps
Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You may modify.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
1 Chapter 3 Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Revised 3/11/05.
Assembly Language for x86 Processors 6th Edition
Assembly language programming
Assembly language.
Format of Assembly language
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
x86 Assembly Language Fundamentals
Assembly Language Fundamentals
Microprocessor and Assembly Language
Assembly IA-32.
Assembly Language for Intel-Based Computers, 5th Edition
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Data-Related Operators and Directives
Assembly Language for Intel-Based Computers
Intel x86 Assembly Fundamentals
Assembler Directives end label end of program, label is entry point
Computer Organization and Assembly Language
Hardware & Software Architecture
Introduction to 8086 Assembly Language
Computer Architecture and System Programming Laboratory
Chapter 10 Instruction Sets: Characteristics and Functions
BASIC SYNTAX OF ASSEMBLY LANGUAGE “HELLO WORLD” PROGRAM.
Presentation transcript:

CS2422 Assembly Language and System Programming 0 Week 7 & 8 Intro. To Assembly Programming

Basic Instruction Set InstructionDescription ADD Adds the two operands and stores the result into the first operand. SUB Subtracts the second operand from the first and stores the result in the first operand. MUL Multiplies the operand with the Accumulator Register and stores the result in the Accumulator Register. DIV Divides the Accumulator Register by the operand and stores the result in the Accumulator Register. AND Performs the bitwise logical AND operation on the operands and stores the result in the first operand. OR Performs the bitwise logical OR operation on the operands and stores the result in the first operand. INC Increases the value of the operand by 1 and stores the result in the operand. DEC Decreases the value of the operand by 1 and stores the result in the operand. The x86 instruction set is extremely huge, but we usually don't need to use them all. Here are some simple instructions you should know to get you started:

Basic Instruction Set … InstructionDescription NEG Negates the operand and stores the result in the operand. PUSH Pushes the value of the operand on to the top of the stack. POP Pops the value of the top item of the stack in to the operand. MOV Stores the second operand's value in the first operand. CMP Subtracts the second operand from the first operand and sets the respective flags. JMP Jumps to label. INT constant Calls the interrupt specified by the operand.

Basic instructions…  mov eax,10000h The MOV instruction moves (copies) the integer 10000h to the EAX register. The first operand (EAX) is called the destination operand, and the second operand is called the source operand.  add eax,40000h The ADD instruction adds 40000h to the EAX register.  sub eax,20000h The SUB instruction subtracts 20000h from the EAX register.

Moving Data into Registers  The first thing we will want to learn about programming at the level of the CPU, is how to put data into registers. For this we use the MOV instruction.  code fragment which moves the number 47 into the EDX register: >> MOV EDX, 47  The following code moves the hexadecimal number A4C9 into the 16 bit AX register: >> MOV AX, 0A4C9h  Similarly, if we wished to move the binary number into the 8 bit BH register we would type the following code: >> MOV BH, b

5 Starting with an Example TITLE Add and Subtract (AddSub.asm) ; Adds and subtracts three 32-bit integers ; (10000h h h) INCLUDE Irvine32.inc.code main PROC mov eax,10000h; EAX = 10000h add eax,40000h; EAX = 50000h sub eax,20000h; EAX = 30000h call DumpRegs; display registers exit main ENDP END main Title/header Include file Code section

6 Meanings of the Code Assembly code Machine code MOV EAX, 10000hB (Move 10000h into EAX) ADD EAX, 40000h (Add 40000h to EAX) SUB EAX, 20000h2D (SUB 20000h from EAX) 6 Operand in instruction

7 Fetched MOV EAX, 10000h 7 … ALU MemoryRegister EAX EBX address 00 … MOV EAX, 10000h SUB EAX, 20000h data PC B IR B ADD EAX, 40000h

8 Execute MOV EAX, 10000h 8 … ALU Register EAX EBX data PC B IR … MOV EAX, 10000h SUB EAX, 20000h B ADD EAX, 40000h Memory address

9 Fetched ADD EAX, 40000h 9 … ALU Register EAX EBX data PC IR Memory address 00 … MOV EAX, 10000h SUB EAX, 20000h B ADD EAX, 40000h

10 Execute ADD EAX, 40000h 10 … ALU Register EAX EBX data PC IR Memory address 00 … MOV EAX, 10000h SUB EAX, 20000h B ADD EAX, 40000h

11 Chapter Overview  Basic Elements of Assembly Language Integer constants and expressions Character and string constants Reserved words and identifiers Directives and instructions Labels Mnemonics and Operands Comments  Example: Adding and Subtracting Integers  Assembling, Linking, and Running Programs  Defining Data  Symbolic Constants  Real-Address Mode Programming

12 Reserved Words, Directives  TITLE: Define program listing title Reserved word of directive  Reserved words Instruction mnemonics, directives, type attributes, operators, predefined symbols See MASM reference in Appendix A  Directives: Commands for assembler TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc.code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

13 Directive vs Instruction assembler  Directives: tell assembler what to do Commands that are recognized and acted upon by the assembler, e.g. declare code, data areas, select memory model, declare procedures, etc. Not part of the Intel instruction set Different assemblers have different directives CPU  Instructions: tell CPU what to do Assembled into machine code by assembler Executed at runtime by the CPU Member of the Intel IA-32 instruction set Format: LABEL (option), Mnemonic, Operands, Comment

14 Comments  Single-line comments begin with semicolon (;)  Multi-line comments begin with COMMENT directive and a programmer-chosen character, end with the same character, e.g. COMMENT ! Comment line 1 Comment line 2 ! TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc.code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

15 Include Files  INCLUDE directive: Copies necessary definitions and setup information from a text file named Irvine32.inc, located in the assembler’s INCLUDE directory (see Chapt 5) TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc.code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

16 Code Segment .code directive: Marks the beginning of the code segment, where all executable statements in a program are located TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc.code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

17 Procedure Definition  Procedure defined by: [label] PROC [label] ENDP  Label: Place markers: marks the address (offset) of code and data Assigned a numeric address by assembler Follow identifier rules Data label: must be unique, e.g. myArray Code label: target of jump and loop instructions, e.g. L1: TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc.code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

18 Identifiers  Identifiers: A programmer-chosen name to identify a variable, a constant, a procedure, or a code label characters, including digits not case sensitive first character must be a letter, ?, or $ TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc.code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

19 Instructions [label:] mnemonic operand(s) [;comment]  Instruction mnemonics: help to memorize examples: MOV, ADD, SUB, MUL, INC, DEC  Operands: constant constant expression register memory (data label, register) TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc.code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main immediate values Destination operand Source operand

20 Instruction Format Examples  No operands stc; set Carry flag nop; no operation  One operand inc eax; register inc myByte; memory  Two operands add ebx,ecx; register, register sub myByte,25; memory, constant add eax,36 * 25; register, constant-expr.

21 I/O  Not easy, if program by ourselves Will use the library provided by the author  Two steps: Include the library (Irvine32.inc) in your code Call the subroutines  call DumpRegs: Calls the procedure to displays current values of processor registers TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc.code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

22 Remaining  exit: Halts the program Not a MSAM keyword, but a command defined in Irvine32.inc  END main: Marks the last line of the program to be assembled Identifies the name of the program ’ s startup procedure TITLE Add and … ; Adds and subtracts ; (10000h + … INCLUDE Irvine32.inc.code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs exit main ENDP END main

23 Example Program Output  Program output, showing registers and flags EAX= EBX=7FFDF000 ECX= EDX=FFFFFFFF ESI= EDI= EBP=0012FFF0 ESP=0012FFC4 EIP= EFL= CF=0 SF=0 ZF=0 OF=0

24 What's Next  Basic Elements of Assembly Language  Example: Adding and Subtracting Integers  Assembling, Linking, and Running Programs  Defining Data  Symbolic Constants  Real-Address Mode Programming

25 Assemble-Link-Execute Cycle  Steps from creating a source program through executing the compiled program

26 Download, Install, and Run  MASM 6.15 (with all examples of textbook) Masm615.zip: download from the course web site  Unzip the archive and run setup.exe  Choose the installation directory Suggest using the default directory See index.htm in the archive for details  Go to C:\Masm615 (if installed default) Write assembly source code ‒ TextPad, NotePad++, UltraEdit or … make32 abc (where abc is your file name) When assembled, just write abc to run the code.

27 Suggestion  Study make32.bat and make16.bat To know where assembling stage and linking stage are  Think about linking with other language (ex: C or C++ or …)  Understand that MASM is only one of the assemblers, and there are still many other assemblers to use Try to use NASM or TASM  Try to use high ‐ level compiler to generate assembly codes gcc or visual c++ or turbo c or …

28 Listing File  Use it to see how your program is compiled  Contains source code addresses object code (machine language) segment names symbols (variables, procedures, and constants)  Example: addSub.lstaddSub.lst

29 What's Next  Basic Elements of Assembly Language  Example: Adding and Subtracting Integers  Assembling, Linking, and Running Programs  Defining Data Intrinsic Data Types Data Definition Statement Defining BYTE, SBYTE, WORD, SWORD, DWORD, SDWORD, QWORD, TBYTE Defining Real Number Data Little Endian Order  Symbolic Constants  Real-Address Mode Programming

30 Intrinsic Data Types BYTE8-bit unsigned integer SBYTE8-bit signed integer WORD16-bit unsigned integer SWORD16-bit signed integer DWORD32-bit unsigned integer SDWORD32-bit signed integer

31 Data Definition Statement  A data definition statement sets aside storage in memory for a variable  May optionally assign a name (label) to the data  Syntax: [name] directive initializer [,initializer]... value1 BYTE 10  All initializers become binary data in memory  Use ? if no initialization necessary Example: Var1 BYTE ?

32 Defining BYTE and SBYTE Data  Each of following defines a single byte of storage: value1 BYTE 'A'; character constant value2 BYTE 0; smallest unsigned byte value3 BYTE 255; largest unsigned byte value4 SBYTE -128; smallest signed byte value5 SBYTE +127; largest signed byte value6 BYTE ?; uninitialized byte

33 Defining Byte Arrays  Examples that use multiple initializers: list1 BYTE 10,20,30,40 list2 BYTE 10,20,30,40 BYTE 50,60,70,80 BYTE 81,82,83,84 list3 BYTE ?,32,41h, b list4 BYTE 0Ah,20h,‘A’,22h

34 Defining Strings  An array of characters Usually enclosed in quotation marks Will often be null-terminated To continue a single string across multiple lines, end each line with a comma str1 BYTE "Enter your name",0 str2 BYTE 'Error: halting program',0 str3 BYTE 'A','E','I','O','U' greeting BYTE "Welcome to the Encryption Demo program " BYTE "created at Tabuk University.",0 menu BYTE "Checking Account",0dh,0ah,0dh,0ah, "1. Create a new account",0dh,0ah, "2. Open an existing account",0dh,0ah, "Choice> ",0 End-of-line sequence: 0Dh = carriage return 0Ah = line feed End-of-line sequence: 0Dh = carriage return 0Ah = line feed

35 Using the DUP Operator  Use DUP to allocate (create space for) an array or string  Syntax: counter DUP (argument)  Counter and argument must be constants or constant expressions var1 BYTE 20 DUP(0); 20 bytes, all equal to zero var2 BYTE 20 DUP(?); 20 bytes, uninitialized var3 BYTE 4 DUP("STACK"); 20 bytes, ; "STACKSTACKSTACKSTACK"

36 Defining WORD and SWORD  Define storage for 16-bit integers or double characters single value or multiple values word1 WORD ; largest unsigned value word2 SWORD –32768; smallest signed value word3 WORD ?; uninitialized, unsigned word4 WORD "AB"; double characters myList WORD 1,2,3,4,5; array of words array WORD 5 DUP(?); uninitialized array

37 Defining Other Types of Data  Storage definitions for 32-bit integers, quadwords, tenbyte values, and real numbers: val1 DWORD h ; unsigned val2 SDWORD – ; signed val3 DWORD 20 DUP(?) ; unsigned array val4 SDWORD –3,–2,–1,0,1; signed array

38 Adding Variables to AddSub TITLE Add and Subtract, Version 2 (AddSub2.asm) ; This program adds and subtracts 32-bit unsigned ; integers and stores the sum in a variable. INCLUDE Irvine32.inc.data val1 DWORD 10000h val2 DWORD 40000h val3 DWORD 20000h finalVal DWORD ?.code main PROC mov eax,val1; start with 10000h add eax,val2; add 40000h sub eax,val3; subtract 20000h mov finalVal,eax; store the result (30000h) call DumpRegs; display the registers exit main ENDP END main