CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 14, FALL 2012

Similar documents
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Programming in C. Pointers and Arrays

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

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

SOEN228, Winter Revision 1.2 Date: October 25,

CPS104 Recitation: Assembly Programming

Computer Architecture and Assembly Language. Practical Session 3

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Computer Systems Organization V Fall 2009

CS-202 Microprocessor and Assembly Language

Computer Architecture and System Programming Laboratory. TA Session 3

Practical Malware Analysis

CMSC 313 Lecture 07. Short vs Near Jumps Logical (bit manipulation) Instructions AND, OR, NOT, SHL, SHR, SAL, SAR, ROL, ROR, RCL, RCR

Reverse Engineering Low Level Software. CS5375 Software Reverse Engineering Dr. Jaime C. Acosta

Language of x86 processor family

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

Lecture 2 Assembly Language

POINTER AND ARRAY SUNU WIBIRAMA

CMSC 313 Lecture 13 Project 4 Questions Reminder: Midterm Exam next Thursday 10/16 Virtual Memory

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

Basic Assembly SYSC-3006

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Intel 8086: Instruction Set

3.1 DATA MOVEMENT INSTRUCTIONS 45

TYPES OF INTERRUPTS: -

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

Arithmetic and Logic Instructions And Programs

CS2141 Software Development using C/C++ C++ Basics

Computer Systems Lecture 9

UW CSE 351, Winter 2013 Midterm Exam

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Lecture (08) x86 programming 7

CS201 - Lecture 1 The C Programming Language

Midterm Exam Review Slides

Introduction to 8086 Assembly

Introduction to 8086 Assembly

Question Points Score Total: 100

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

Basic Pentium Instructions. October 18

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Defining and Using Simple Data Types

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

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Assembler Programming. Lecture 10

Subprograms: Arguments

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Lesson #8. Structures Linked Lists Command Line Arguments

211: Computer Architecture Summer 2016

Ex : Write an ALP to evaluate x(y + z) where x = 10H, y = 20H and z = 30H and store the result in a memory location 54000H.

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

CMSC 313 Lecture 05 [draft]

Assembly Language: IA-32 Instructions

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

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Procedures, Parameters, Values and Variables. Steven R. Bagley

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

Representation of Information

Instructions and Instruction Set Architecture

CSE351 Spring 2018, Midterm Exam April 27, 2018

IBM PC Hardware CPU 8088, Pentium... ALU (Arithmetic and Logic Unit) Registers. CU (Control Unit) IP.

Algorithms & Programming. Midterm Examination # 2. Student Name: Student ID Number: Duration of examination:

Data Representation and Storage. Some definitions (in C)

Week /8086 Microprocessor Programming I

ECE264 Fall 2013 Exam 1, September 24, 2013

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

University of California, San Diego CSE 30 Computer Organization and Systems Programming Winter 2014 Midterm Dr. Diba Mirza

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

Assembly Language II: Addressing Modes & Control Flow

Lecture 9. INC and DEC. INC/DEC Examples ADD. ADD Examples SUB. INC adds one to a single operand DEC decrements one from a single operand

Logic Instructions. Basic Logic Instructions (AND, OR, XOR, TEST, NOT, NEG) Shift and Rotate instructions (SHL, SAL, SHR, SAR) Segment 4A

8086 Programming. Multiplication Instructions. Multiplication can be performed on signed and unsigned numbers.

CS , Spring 2004 Exam 1

EE 312 Fall 2017 Midterm 1 October 12, 2017

Instructions moving data

Class Information ANNOUCEMENTS

CMSC 313 Lecture 14 Reminder: Midterm Exam next Tues (10/26) Project 4 Questions Virtual Memory on Linux/Pentium platform

Roadmap: Security in the software lifecycle. Memory corruption vulnerabilities

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

PROGRAMMAZIONE I A.A. 2017/2018

EL2310 Scientific Programming

HW1 due Monday by 9:30am Assignment online, submission details to come

Exercise Session 2 Simon Gerber

CPSC 213, Winter 2016, Term 2 Final Exam Date: April 19, 2017; Instructor: Mike Feeley and Alan Wagner

CPSC 213, Winter 2009, Term 2 Midterm Exam Date: March 12, 2010; Instructor: Mike Feeley

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

CS270 Homework 5 (HW5)

C & Data Structures syllabus

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1

Transcription:

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 14, FALL 2012

TOPICS TODAY Midterm exam topics Recap arrays vs pointers Characters & strings & pointers (Oh My!) Structs & pointers

MIDTERM EXAM TOPICS

MIDTERM EXAM Tuesday, October 23 In Class No Calculators, cell phones, electronics,...

MIDTERM FORMAT Multiple Choice Short responses (e.g., base conversion) Trace assembly language program Write assembly language program Full text of toupper.asm available

MIDTERM TOPICS Base Conversion Data Representation negative numbers: 2's complement, 1's complement, signed magnitude ASCII little endian vs big endian Intel CPU Registers Addressing modes Flags Common instructions

Basic Instructions Common Instructions ADD, SUB, INC, DEC, MOV, NOP Branching Instructions JMP, CMP, Jcc More Arithmetic Instructions NEG, MUL, IMUL, DIV, IDIV Logical (bit manipulation) Instructions AND, OR, NOT, SHL, SHR, SAL, SAR, ROL, ROR, RCL, RCR Subroutine Instructions PUSH, POP, CALL, RET UMBC, CMSC313, Richard Chang <chang@umbc.edu>

MIDTERM TOPICS (CONT'D) Comparison & conditional jump instructions signed vs unsigned conditional jumps (e.g. ja vs jg) NASM How to assemble.data,.bss,.text sections dd, dw, db, resd, resw, resb directives %define! System calls for read & write Separate compilation, linking & loading Interrupts (general principles)

RECAP ARRAYS VS. POINTERS

C Parameter Passing Notes We'll say formal parameter vs actual parameter. Formal parameters are place holders in function definition. Actual parameters (aka arguments) actually have a value. In C, all parameters are passed by value. Parameter passing by reference is simulated by passing the address of the variable. scanf("%d", &n) ;! Array names represent the address of the array. In effect, arrays are passed by reference. int UpdateArray (int A[], int n) {! A[0] += 5 ;!...!

Printing an Array The code below shows how to use a parameter array name as a pointer. void printgrades( int grades[ ], int size ) { int i; for (i = 0; i < size; i++) printf( %d\n, *grades ); ++grades; } What about this prototype? void printgrades( int *grades, int size );

Passing Arrays Arrays are passed by reference (its address is passed by value):!!!int sumarray( int A[], int size) ;! is equivalent to!int sumarray( int *A, int size) ;! Use A as an array name or as a pointer. The compiler always sees A as a pointer. In fact, any error messages produced will refer to A as an int *

sumarray int sumarray( int A[ ], int size) { } int k, sum = 0; for (k = 0; k < size; k++) sum += A[ k ]; return sum;

sumarray (2) int sumarray( int A[ ], int size) { int k, sum = 0; for (k = 0; k < size; k++) sum += *(A + k); return sum; } int sumarray( int A[ ], int size) { int k, sum = 0; for (k = 0; k < size; k++) } sum += *A; ++A; } return sum; }

CHARACTERS & STRINGS & POINTERS

Strings revisited Recall that a string is represented as an array of characters terminated with a null (\0) character. As we ve seen, arrays and pointers are closely related. A string constant may be declared as either as in char[ ] or char * char hello[ ] = Hello Bobby ; or (almost) equivalently char *hi = Hello Bob ; A typedef could also be used to simplify coding typedef char* STRING; STRING hi = Hello Bob ;

Arrays of Pointers Since a pointer is a variable type, we can create an array of pointers just like we can create any array of any other type. Although the pointers may point to any type, the most common use of an array of pointers is an array of char* to create an array of strings.

Boy s Names A common use of an array of pointers is to create an array of strings. The declaration below creates an initialized array of strings (char *) for some boys names. The diagram below illustrates the memory configuration. char *name[] = { Bobby, Jim, Harold }; name: 0 1 2 B o b b y \0 J i m \0 H a r o l d \0

Command Line Arguments Command line arguments:./a.out breakfast lunch dinner! These arguments are passed to your program as parameters to main. int main( int argc, char *argv[ ] )! argc is the number of command line arguments argv is an array of argc strings argv[0]is always the name of your executable program. The rest of argv[] are the remaining strings on the command line.

Command Line Arguments (2) Example, with this command at the Linux prompt: we get myprog hello world 42! argc = 4! argv[0] = "myprog"! argv[1] = "hello"! argv[2] = "world"! argv[3] = "42"! Note: argv[3] is a string NOT an integer. Convert using atoi(): int answer = atoi( argv[3] );!

STRUCTS & POINTERS

Reminder You can t use a pointer until it points to something Just declaring a variable to be a pointer is not enough int *name; /* pointer declaration */ int age = 42; *name += 12; printf( My age is %d\n, *name);

Pointers to Pointers A pointer may point to another pointer. Consider the following declarations int age = 42;!! /* an int */! int *page = &age;!/* a pointer to an int */! int **ppage = &page;/* pointer to pointer to int */! Draw a memory picture of these variable and their relationships What type and what value do each of the following represent?! age, page, ppage, *page, *ppage, **ppage!

pointers2pointer.c int main ( )! {!!/* a double, a pointer to double,!!** and a pointer to a pointer to a double */!!double gpa = 3.25, *pgpa, **ppgpa;!!!!/* make pgpa point to the gpa */!!pgpa = &gpa;!!!!/* make ppgpa point to pgpa (which points to gpa) */!!ppgpa = &pgpa;!!!!// what is the output from this printf statement?!!printf( "%0.2f, %0.2f, %0.2f", gpa, *pgpa, **ppgpa);!!!!return 0;! }!

typedef struct student { char name[50]; char major [20]; double gpa; } STUDENT; Pointers to struct STUDENT bob = {"Bob Smith", "Math", 3.77}; STUDENT sally = {"Sally", "CSEE", 4.0}; STUDENT *pstudent; /* pstudent is a "pointer to struct student" */ pstudent = &bob; /* make pstudent point to bob */ /* use -> to access the members */ printf ("Bob's name: %s\n", pstudent->name); printf ("Bob's gpa : %f\n", pstudent->gpa); /* make pstudent point to sally */ pstudent = &sally; printf ("Sally's name: %s\n", pstudent->name); printf ("Sally's gpa: %f\n", pstudent->gpa);

Pointer in a struct The data member of a struct can be a pointer #define FNSIZE 50! #define LNSIZE 40! typedef struct name! {!!char first[ FNSIZE + 1 ];!!char last [ LNSIZE + 1 ];! } NAME;!! typedef struct person! {!!NAME *pname;!// pointer to NAME struct!!int age;!!double gpa;! } PERSON;!

Pointer in a struct (2) Given the declarations below, how do we access bob s name, last name, and first name? Draw a picture of memory represented by these declarations NAME bobsname = { Bob, Smith };! PERSON bob;! bob.age = 42;! bob.gpa = 3.4;! bob.pname = &bobsname;!!

Self-referencing structs Powerful data structures can be created when a data member of a struct is a pointer to a struct of the same kind. The simple example on the next slide illustrates the technique.

teammates.c typedef struct player { char name[20]; struct player *teammate;/* can t use TEAMMATE yet */ } TEAMMATE; TEAMMATE *team, bob, harry, john; team = &bob; /* first player */ strncpy(bob.name, bob, 20); bob.teammate = &harry; /* next teammate */ strncpy(harry.name, harry, 20); harry.teammate = &john; /* next teammate */ strncpy(john.name, bill, 20); john.teammate = NULL: /* last teammate */

teammates.c (cont d) /* typical code to print a (linked) list */ /* follow the teammate pointers until ** NULL is encountered */ // start with first player TEAMMATE *t = team; // while there are more players... while (t!= NULL) { printf( %s\n, t->name); } // next player t = t->teammate;

NEXT TIME Dynamic memory allocation