Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.

Slides:



Advertisements
Similar presentations
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
Advertisements

1 Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Flow Control Instructions
Assembly Language for Intel-Based Computers Chapter 3: Assembly Language Fundamentals Kip Irvine.
Practical Session 2. Labels Definition valid characters in labels are: letters, numbers, _, $, ~,., and ? first character can be: letter, _, ? and.
8051 ASSEMBLY LANGUAGE PROGRAMMING
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Practical Session 8 Computer Architecture and Assembly Language.
1 Homework Reading –PAL, pp Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section.
CS2422 Assembly Language & System Programming September 26, 2006.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
Joseph L. Lindo Assembly Programming Sir Joseph Lindo University of the Cordilleras.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Chapter 2 Software Tools and Assembly Language Syntax.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
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.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Faculty of Engineering, Electrical Department,
Dr. José M. Reyes Álamo 1.  Review: ◦ Statement Labels ◦ Unconditional Jumps ◦ Conditional Jumps.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
The x86 PC Assembly Language, Design, and Interfacing By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey © 2010, 2003, 2000, 1998 Pearson.
Computer Architecture and Assembly Language. Byte structure : a byte has 8 bits MSB (Most Significant Bit) LSB (Least Significant Bit) Data Representation.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
26-Nov-15 (1) CSC Computer Organization Lecture 6: Pentium IA-32.
Click to add Title Comunicación y Gerencia Click To add Subtitle Click to add Text Fundamentals of Assembly Language.
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.
Chapter 5 Branching and Looping.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set.
Review of Assembly language. Recalling main concepts.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
Computer Architecture and Assembly Language
Assembly 09. Outline Strings in x86 esi, edi, ecx, eax stosb, stosw, stosd cld, std rep loop 1.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Practical Session 8. Position Independent Code- self sufficiency of combining program Position Independent Code (PIC) program has everything it needs.
Chapter 8 String Operations. 8.1 Using String Instructions.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Microprocessor and Assembly Language
Microprocessor and Assembly Language
Assembly IA-32.
Assembly Language Programming Part 2
CS 301 Fall 2002 Control Structures
Computer Organization and Assembly Language
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Practical Session 2.
Data Transfers, Addressing, and Arithmetic
Shift & Rotate Instructions)
Homework Reading Machine Projects Labs PAL, pp
Computer Architecture and Assembly Language
University of Gujrat Department of Computer Science
Computer Architecture and System Programming Laboratory
Assembly Language for Intel 8086 Jump Condition
Some Assembly (Part 2) set.html.
Computer Organization and Assembly Language
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Presentation transcript:

Assembly 03

Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1

inc Command inc increments the operand by one mov eax, 0 inc eax 2 eax

inc Command inc increments the operand by one mov eax, 0 inc eax 3 0 eax

inc Command inc increments the operand by one mov eax, 0 inc eax 4 1 eax

inc Command inc increments the operand by one mov eax, 0 inc eax 5 2 eax

inc Command inc increments the operand by one mov eax, 0 inc eax 6 3 eax

dec Command dec decrements the operand by one mov eax, 7 dec eax 7 eax

dec Command dec decrements the operand by one mov eax, 7 dec eax 8 eax 7

dec Command dec decrements the operand by one mov eax, 7 dec eax 9 eax 6

dec Command dec decrements the operand by one mov eax, 7 dec eax 10 eax 5

dec Command dec decrements the operand by one mov eax, 7 dec eax 11 eax 4

Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 12

movsx Command movsx -> mov with sign extension movsx preserves the signed (negative) value when moving: 8bits -> 16 bits 8 bits -> 32 bits 16 bits -> 32 bits 13

movsx Command mov ax, -42; mov ebx, eax 14 eax ebx ax

movsx Command mov ax, -42; mov ebx, eax 15 eax ebx 16-bit 2’s complement interpreted as -42 0xFFD6 ax

movsx Command mov ax, -42; mov ebx, eax 16 eax 0x0000FFD6 ebx 0xFFD6 ax 32-bit 2’s complement interpreted as 65,494

movsx Command Now use the movsx command.. mov ax, -42; movsx ebx, ax 17 ebx ax eax ax Note: we are extending from 16 to 32 bits

movsx Command mov ax, -42; movsx ebx, ax 18 ebx ax eax 0xFFD6 ax

movsx Command mov ax, -42; movsx ebx, ax 19 0xFFFFFFD6 ebx ax eax 0xFFD6 ax 32-bit 2’s complement interpreted as -42

Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 20

jmp Command jmp ->(always) “jump to label” mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 21 label

jmp Command mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 22 1 eax ebx

jmp Command mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 23 1 eax 1 ebx

jmp Command mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 24 1 eax 1 ebx jump to skip

jmp Command mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 25 2 eax 1 ebx

jnz Command jnz -> “jump if not zero” Checks EFLAGS ZF (zero flag, bit #6) Previous instruction may modify ZF bit Note: Not all instructions modify ZF bit! If ZF clear Jump to label If ZF set Continue with next instruction 26

jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 27 eax ZF (bit #6 of EFLAGS)

jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 28 3 eax 0 ZF (bit #6 of EFLAGS)

jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 29 2 eax 0 ZF (bit #6 of EFLAGS)

jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 30 2 eax 0 ZF (bit #6 of EFLAGS) 1) ZF is clear 2) jump to loop label

jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 31 1 eax 0 ZF (bit #6 of EFLAGS)

jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 32 1 eax 0 ZF (bit #6 of EFLAGS) 1) ZF is clear 2) jump to loop label

jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 33 0 eax 1 ZF (bit #6 of EFLAGS) result of instruction sets ZF

jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 34 0 eax 1 ZF (bit #6 of EFLAGS) 1) ZF is set 2) execution continues

jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 35 0xBEEF eax 1 ZF (bit #6 of EFLAGS) ZF unchanged after mov command…Why?

Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 36

nasm Assembly Program Sections 1..data 2..bss 3..text 37

nasm Assembly Program Sections.data Declare data items (variables) in memory Data items can be initialized with value Data items stored in memory with executable 38

nasm Assembly Program Sections.bss Declare data buffers (variables) in memory Data buffers NOT initialized with a value Great for large data buffers (e.g., 1,000 bytes) Data buffers NOT stored with executable More efficient than.data section Variables not loaded in memory with executable 39

nasm Assembly Program Sections.text Instructions (mnemonics and operands) No data items defined in.text (normally) Contains labels for locations 40

Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 41

Labels “Bookmark” for program code Place to jump to Name for procedure Label must start with a letter, underscore ‘_’, period ‘.’, or question mark ‘?’ Label is case sensitive Label ends with colon ‘:’ 42

Labels _start is a special kind of Label _start similar to main() in C/C++ _start must go at beginning of.text section Required by Linux to load executable 43

Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 44

String Variables E.g., EatMsg: db “Eat at Joe’s!”, 10 45

String Variables E.g., EatMsg: db “Eat at Joe’s!”, Label

String Variables E.g., EatMsg: db “Eat at Joe’s!”, db - “declare byte” - interpret as 8-bits

String Variables E.g., EatMsg: db “Eat at Joe’s!”, hard-coded string of characters - can use double or single quotes - assembler decides how much memory to set aside

String Variables E.g., EatMsg: db “Eat at Joe’s!”, comma concatenates substrings

String Variables E.g., EatMsg: db “Eat at Joe’s!”, decimal number for end of line (EOL) character (basically a newline)

Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 51

equ Command equ -> “equate” Way to associate a value with a label equ is an assembly-time calculation Command is executed during assembly Similar to #define in C / C++ 52

equ Command.data SIZE: equ 10;.text _start mov eax, 10; mov eax, SIZE; 53 these instructions are the same!

equ Command Why use equ? Increased code readability E.g., SIZE vs. 10 Ability to clearly define constants Easier to maintain code What if SIZE changes from 10 to 12? Only need to modify one line of code 54

Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 55

$ Token In x86 assembly language, $ token means “here” EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 56

$ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 57 EatLen equates to…

$ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 58 …“here” minus EatMsg

$ Token EatMsg is a Label, which is just an address $ or here is also an address Current address of instruction in memory Since assembler sets aside bytes.. we can determine the string length.. 59

$ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 60 LabelAddressByte EatMsg:1008E 1009a 1010t a 1013t J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022

$ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 61 LabelAddressByte EatMsg:1008E 1009a 1010t a 1013t J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 EatMsg = 1008

$ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 62 LabelAddressByte EatMsg:1008E 1009a 1010t a 1013t J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 String requires 14 bytes of memory

$ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 63 LabelAddressByte EatMsg:1008E 1009a 1010t a 1013t J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 $ (here) is now address 1022 of memory

$ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 64 LabelAddressByte EatMsg:1008E 1009a 1010t a 1013t J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 $ = 1022 EatMsg = 1008 EatLen = $ - EatMsg = 1022 – 1008 = 14

$ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 65 LabelAddressByte EatMsg:1008E 1009a 1010t a 1013t J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 EatLen = 14

$ Token Now EatLen is flexible EatLen will be the correct size, regardless of EatMsg 66