mith College Computer Science CSC231 Assembly Week #9 Fall 2017 Dominique Thiébaut

Similar documents
mith College Computer Science CSC231 - Assembly Week #9 Dominique Thiébaut

mith College Computer Science CSC231 - Assembly Week #3 Dominique Thiébaut

mith College Computer Science CSC231 - Assembly Week #8 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut

mith College Computer Science CSC231-Assembly Fall 2017 Week #2 Dominique Thiébaut

mith College Computer Science CSC231 - Assembly Week #4 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #5 Fall 2017 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #12 Thanksgiving 2017 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

X86-NASM STANDARD COMMANDS. Comment your code with a semicolon (;)! The assembler won t read anything after it.

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

mith College Computer Science CSC231 Assembly Week #10 Fall 2017 Dominique Thiébaut

ADDRESSING MODES. Operands specify the data to be used by an instruction

Computer Architecture and Assembly Language. Practical Session 5

mith College Computer Science CSC231 Assembly Week #13 Fall 2017 Dominique Thiébaut

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

Machine and Assembly Language Principles

CMSC 313 Lecture 08 Project 2 Questions Recap Indexed Addressing Examples Some i386 string instructions A Bigger Example: Escape Sequence Project

Chapter 11. Addressing Modes

mith College Computer Science Fixed & Floating Point Formats CSC231 Fall 2017 Week #15 Dominique Thiébaut

printf("this program adds the value 10 to a given integer number.\n\n");

Lab 3. The Art of Assembly Language (II)

mith College Computer Science Fixed-Point & Floating-Point Number Formats CSC231 Dominique Thiébaut

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

The Instruction Set. Chapter 5

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

ASSEMBLY - QUICK GUIDE ASSEMBLY - INTRODUCTION

Assembly Language Programming Debugging programs

Name: CMSC 313 Fall 2001 Computer Organization & Assembly Language Programming Exam 1. Question Points I. /34 II. /30 III.

mith College Computer Science Fixed-Point & Floating-Point Number Formats CSC231 Assembly Language Week #13 Dominique Thiébaut

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Computer Architecture and Assembly Language. Practical Session 3

x86 assembly CS449 Spring 2016

Assembly Language Each statement in an assembly language program consists of four parts or fields.

Machine Code and Assemblers November 6

Introduction to 8086 Assembly

Introduction to 8086 Assembly

Assembly Language Lab # 9

Assembly basics CS 2XA3. Term I, 2017/18

LAB 5 Arithmetic Operations Simple Calculator

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Process Layout and Function Calls

Assembly III: Procedures. Jo, Heeseung

The x86 Architecture

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Load Effective Address Part I Written By: Vandad Nahavandi Pour Web-site:

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Lab 4: Basic Instructions and Addressing Modes

CSC 8400: Computer Systems. Machine-Level Representation of Programs

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

Computer Architecture and System Programming Laboratory. TA Session 5

Ethical Hacking. Assembly Language Tutorial

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

Computer Organization & Assembly Language Programming

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

x86 assembly CS449 Fall 2017

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Module 3 Instruction Set Architecture (ISA)

Lab 6: Conditional Processing

16.317: Microprocessor Systems Design I Fall 2014

Assembler Programming. Lecture 2

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

Addressing Modes. Outline

Representation of Information

Experiment 8 8 Subroutine Handling Instructions and Macros

CS Basics 8) Strings. Emmanuel Benoist. Fall Term Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 1

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

mith College Computer Science Fixed-Point & Floating-Point Number Formats CSC231 Assembly Language Week #14 Dominique Thiébaut

Chapter 3: Addressing Modes

Addressing Modes on the x86

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Instruction Set Architectures

CSC201, SECTION 002, Fall 2000: Homework Assignment #3

Lecture 2 Assembly Language

Lab 3: Defining Data and Symbolic Constants

UMBC. 1 (Feb. 9, 2002) seg_base + base + index. Systems Design & Programming 80x86 Assembly II CMPE 310. Base-Plus-Index addressing:

Assembly Language: Function Calls

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS Basics 5) Programming in Assembly Language

register clicker:

Instruction Set Architectures

CMSC 313 Lecture 08. Project 2 due date moved to Thursday 9/25 Project 3 to be assigned Thursday 9/25, still due Tuesday 10/7

U23 - Binary Exploitation

Language of x86 processor family

StarForce 3 - Brief insight into a hidden world. By [yates] [ [

Assembly Language: Function Calls" Goals of this Lecture"

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 03, SPRING 2013

Program Exploitation Intro

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Practical Malware Analysis

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language Programming

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Machine-Level Programming I: Introduction Jan. 30, 2001

Transcription:

mith College Computer Science CSC231 Assembly Week #9 Fall 2017 Dominique Thiébaut dthiebaut@smith.edu

Looping Through Arrays

LOOP INSTRUCTION Looping Through Arrays INDIRECT ADDRESSING MODE

Indirect Addressing Mode The addressing mode refers to the way the operand of an instruction is generated. We already know register mode, immediate mode, and direct mode.

Tracing One Example of Indirect Addressing (Base Addressing)

ebx??? 0x1104C 0x1104B Memory 12 33 al section.data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12? 0x1104A 0x11049 0x11048 78 56 3E section.text _start: mov al, 'z' mov ebx, A mov byte[ebx], 0 0x11047 0x11046 F0 3 mov ebx, B mov byte[ebx], al 0x11045 1

ebx??? 0x1104C 0x1104B Memory 12 33 al section.data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12 'z' 0x1104A 0x11049 0x11048 78 56 3E section.text _start: mov al, 'z' mov ebx, A mov byte[ebx], 0 0x11047 0x11046 F0 3 mov ebx, B mov byte[ebx], al 0x11045 1

0x1104C 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 12 33 78 56 3E F0 3 1 ebx al section.data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12 section.text _start: mov al, 'z' mov ebx, A mov byte[ebx], 0 mov ebx, B mov byte[ebx], al 11045 'z'

0x1104C 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 12 33 78 56 3E F0 3 1 0 ebx al section.data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12 section.text _start: mov al, 'z' mov ebx, A mov byte[ebx], 0 mov ebx, B mov byte[ebx], al 11045 'z'

0x1104C 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 12 33 78 56 3E F0 3 1 0 ebx al section.data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12 section.text _start: mov al, 'z' mov ebx, A mov byte[ebx], 0 mov ebx, B mov byte[ebx], al 1104A 'z'

0x1104C 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 12 33 78 'z' 56 3E F0 3 1 0 ebx al section.data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12 section.text _start: mov al, 'z' mov ebx, A mov byte[ebx], 0 mov ebx, B mov byte[ebx], al 1104A 'z'

Example 2: Setting an Array to All 0s

; Array Table contains 10 words Table dw 1,2,3,4,5,6 dw 7,8,9,10 mov ecx, ;# of elements mov ebx, ;address of ;Table clear: mov word[ebx], ;value to store add ebx, ;make ebx point ;to next word loop clear ;ecx< ecx-1 ;if ecx!=0, ; goto clear

Exercises Problem #1: Store the sequence 1,2, 3, 4, 10 into an array of 10 ints using a loop. Problem #2: Given a DNA sequence of 1,000,000 characters stored in an array of bytes, and all characters in uppercase, transform it into its lowercase equivalent. The characters are A, C, G, T and N. How long would this take on a 1GHz processor?

1,000,000 DNA Bases: How fast? section.data DNA db "AGCTANATTTTAGC " db "GGTC " db "GCCCTTTTAAAA" N equ 1000000 mov ebx, DNA ; ebx points to DNA mov ecx, N ; ready to loop N times for: add byte[ebx], -'A'+'a' ; transform char to lowercase inc ebx ; ebx points to next byte loop for ; loop N times

1,000,000 DNA Bases: How fast? DNA db "AGCTANATTTTAGC " db "GGTC " db "GCCCTTTTAAAA" N equ 1000000 1 1 1 1 1 mov ebx, DNA ; ebx points to DNA mov ecx, N ; ready to loop N times for: add byte[ebx], -'A'+'a' ; transform char to lowercase inc ebx ; ebx points to next byte loop for ; loop N times Total # cycles = 2 + 3*1,000,000 = 3,000,002 cycles Assuming frequency of 1GHz, 1 cycle = 1ns 3,000,0002 ns = 0.003 sec

1,000,000 DNA Bases: How fast? N equ 1000000 section.bss DNA resb N Another way to reserve bytes in memory section.text ; some code goes here to fill DNA with actual letters mov ebx, DNA ; ebx points to DNA mov ecx, N ; ready to loop N times for: add byte[ebx], -'A'+'a' ; transform char to lowercase inc ebx ; ebx points to next byte loop for ; loop N times

We stopped here last time

Announcement Friday Lab: Video Introduction

Understanding Indirect Addressing

Index 1001396 1101395 1101394 1101393 1101392 1101391 1101390 1101389 1101388 'A' 'A' 'T' 'G' 'C' 'T' 'T' 'G' 'G' JAVA Java Array 0 'A' DNA

Index 1001396 1101395 'A' 'A' Store 'G' at Index 1101390 D. Thiebaut, Computer Science, CollegeJAVA 1101394 'T' 1101393 'G' 1101392 'C' 1101391 'T' 1101390 'T' 1101389 'G' 1101388 'G' Java Array 0 'A' DNA Smith

Index 1001396 1101395 'A' 'A' Store 'G' at Indexes 1101388 to 1101396 D. Thiebaut, Computer Science, CollegeJAVA 1101394 'T' 1101393 'G' 1101392 'C' 1101391 'T' 1101390 'T' 1101389 'G' 1101388 'G' Java Array 0 'A' DNA Smith

JAVA ASM

Addresses 1001396 'A' 1101395 'A' D. Thiebaut, Computer Science, CollegeASM 1101394 'T' 1101393 'G' 1101392 'C' 1101391 'T' 1101390 'T' 1101389 'G' 1101388 'G' DNA 0 Nasm label Smith

Addresses 1001396 1101395 'A' 'A' Store 'G' at Address 1101390 D. Thiebaut, Computer Science, CollegeASM 1101394 'T' 1101393 'G' 1101392 'C' 1101391 'T' 1101390 'T' 1101389 'G' 1101388 'G' DNA 0 Nasm label Smith

1001396 1101395 'A' 'A' Addresses Store 'G' at Addresses 1101388 to 1101396 D. Thiebaut, Computer Science, CollegeASM 1101394 'T' 1101393 'G' 1101392 'C' 1101391 'T' 1101390 'T' 1101389 'G' 1101388 'G' DNA 0 Nasm label Smith

Addressing Modes

Immediate Direct Indirect Indirect plus Displacement Indirect Indexed Indirect Indexed plus Displacement

Immediate Direct Indirect Indirect plus Displacement Indirect Indexed Indirect Indexed plus Displacement

Immediate mov ax, 0x1122 eax XXXXXXXX Before

Immediate mov ax, 0x1122 eax XXXXXXXX eax XXXX1122 Before After

Immediate Direct Indirect Indirect plus Displacement Indirect Indexed Indirect Indexed plus Displacement

Direct Memory 0x1104B 33 0x1104A 78 0x11049 56 mov eax, dword[a] 0x11048 3E < a 0x11047 F0 0x11046 3 0x11045 1 eax 00000000 Before

Direct Memory 0x1104B 33 0x1104A 78 0x11049 56 mov eax, dword[a] 0x11048 3E < a 0x11047 F0 0x11046 3 0x11045 1 eax 00000000 eax 3378563E Before After

Immediate Direct Indirect Indirect plus Displacement Indirect Indexed Indirect Indexed plus Displacement

Memory Indirect 0x1104B 33 0x1104A 78 0x11049 56 mov ebx, a mov eax, dword[ebx] 0x11048 0x11047 3E F0 < a 0x11046 3 0x11045 1 eax XXXXXXXX ebx a Before

Memory Indirect 0x1104B 33 0x1104A 78 0x11049 56 mov ebx, a mov eax, dword[ebx] 0x11048 0x11047 3E F0 < a 0x11046 3 0x11045 1 eax XXXXXXXX eax 3378563E ebx a Before After

Immediate Direct Indirect Indirect plus Displacement Indirect Indexed Indirect Indexed plus Displacement

Indirect plus Dispt. mov ebx, a mov eax, dword[ebx+3] 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 33 78 56 3E F0 3 1 < a eax ebx 00000000 a Before

Indirect plus Dispt. Memory 0x1104B 33 mov ebx, a mov eax, dword[ebx+3] + 0x1104A 0x11049 0x11048 78 56 3E 0x11047 F0 0x11046 0x11045 3 1 < a eax ebx 00000000 a eax 3378563E Before After

Immediate Direct Indirect Indirect plus Displacement Indirect Indexed Indirect Indexed plus Displacement

2 New Registers! eax ebx ecx edx

2 New Registers! eax eip ebx ecx edx

2 New Registers! eax ebx ecx eip esi edi edx "i" in esi, edi for index "s" for source, "d" for destination

2 New Registers! eax ebx ecx eip esi edi Data Registers edx Index Registers (By the way, we can use esi and edi in indirect mode)

Immediate Direct Indirect Indirect plus Displacement Indirect Indexed Indirect Indexed plus Displacement

Indirect Indexed Memory mov ebx, a mov esi, 2 mov ax, word[ebx+esi] 0x1104B 0x1104A 0x11049 0x11048 0x11047 33 78 56 3E F0 0x11046 0x11045 3 1 < a eax ebx esi XXXXXXXX a 2 Before

Indirect Indexed Memory mov ebx, a mov esi, 2 mov ax, word[ebx+esi] 0x1104B 0x1104A 0x11049 0x11048 0x11047 33 78 56 3E F0 0x11046 0x11045 3 1 < a eax ebx XXXXXXXX a + esi 2 eax XXXX3EF0 Before After

Immediate Direct Indirect Indirect plus Displacement Indirect Indexed Indirect Indexed plus Displacement

Indirect Indexed plus Displacement mov ebx, a mov esi, 2 mov ax, word[ebx+esi+1] 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 33 78 56 3E F0 3 1 < a eax ebx esi XXXXXXXX a 2 Before

Indirect Indexed plus Displacement mov ebx, a mov esi, 2 mov ax, word[ebx+esi+1] 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 33 78 56 3E F0 3 1 < a eax XXXXXXXX + ebx a esi 2 eax XXXX563E Before After

msg1 db "A man, a plan, a canal, Panama" msg2 db " " MSGLEN equ $-msg2 mov mov mov esi, msg1 edi, msg2+msglen-1 ecx, MSGLEN for mov al, byte[esi] mov byte[edi], al inc esi dec edi loop for

Solution 2 msg db "HELLOTHEREHOWAREYOU" MSGLEN equ $-msg mov ebx, msg ; ebx points to 1st char of msg mov ecx, MSGLEN ; # of chars in string for: sub byte[ebx],32 ; lower to upper case, in memory inc ebx ; ebx points to next char loop for

Solution 3 Table db 0,0,0,0,0,0,0,0 N equ 8 mov ebx, Table ; ebx points to Table[0] mov esi, 0 ; esi is index 0 mov al, 1 ; first power of 2 mov ecx, N ; number of items in Table for: mov byte[ebx+esi], al ; Table[i] <- al inc esi ; point to next uncomputed fib add al, al ; al <- 2*al loop for ; go back

Solution 4 fib dw 0, 0, 0, 0, 0, 0, 0, 0 dw 0, 0, 0, 0, 0, 0, 0, 0 NOFIB equ 16 mov word[fib], 1 ; init fib[0] mov word[fib+2], 1 ; init fib[1] mov ebx, fib ; ebx points to fib mov esi, 2*2 ; esi is index 2 mov ecx, NOFIB-2 ; compute 14 fib in loop mov ax, word[ebx+esi-1*2] ; ax <- fib[0] for: mov ax, word[ebx+esi-1*2] add ax, word[ebx+esi-2*2] ; ax <- fib[0]+fib[1] mov word[ebx+esi], ax ; fib[i] <- ax add esi,2 ; esi now index of next fib loop for

Solution 5 Powers dd 0,0,0,0,0,0,0,0,0,0 NOPOW equ 10 mov ebx, Powers ; ebx points to powers mov eax, 1 mov ecx, NOPOW ; ready to loop 9 times for: mov dword[ebx], eax ; cell of array add ebx, 4 ; point to next empty cell add eax, eax ; eax <- eax * 2 loop for ; go 10 times

Solution 6 msg1 db "A man, a plan, a canal, Panama" msg2 db " " MSGLEN equ $-msg2 mov ebx, msg1 mov esi, 0 mov edi, msg2+msglen-1 mov ecx, MSGLEN for mov al, byte[ebx+esi] mov byte[edi], al inc esi dec edi loop for