Practical Session 5. Addressing Modes #include #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int DotProduct(int V1[VECTOR_SIZE],

Slides:



Advertisements
Similar presentations
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY.
Advertisements

C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
Practical Session 3 Computer Architecture and Assembly Language.
Practical Session 5. Addressing Modes #include #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int DotProduct(int V1[VECTOR_SIZE],
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Practical Session 5. Addressing Modes #include #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int DotProduct(int V1[VECTOR_SIZE],
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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.
Accessing parameters from the stack and calling functions.
Practical Session 5. Addressing Modes #include #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int DotProduct(int V1[VECTOR_SIZE],
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.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Position Independent Code self sufficiency of combining program.
Practical Session 8 Computer Architecture and Assembly Language.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
6.828: PC hardware and x86 Frans Kaashoek
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Practical Session 7 Computer Architecture and Assembly Language.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
IA32 (Pentium) Processor Architecture. Processor modes: 1.Protected (mode we will study) – 32-bit mode – 32-bit (4GB) address space 2.Virtual 8086 modes.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
CNIT 127: Exploit Development Ch 3: Shellcode. Topics Protection rings Syscalls Shellcode nasm Assembler ld GNU Linker objdump to see contents of object.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
1 ICS 51 Introductory Computer Organization Fall 2009.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Practical Session 5 Computer Architecture and Assembly Language.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Computer Architecture and Assembly Language
Assembly 09. Outline Strings in x86 esi, edi, ecx, eax stosb, stosw, stosd cld, std rep loop 1.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Practical Session 8. Position Independent Code- self sufficiency of combining program Position Independent Code (PIC) program has everything it needs.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
Practical Session 5 Computer Architecture and Assembly Language.
IA32 addressing modes 1.Immediate 2.Direct memory 3.Register 4.Register indirect 5.Indexed 6.Based-indexed.
Practical Session 3 Computer Architecture and Assembly Language.
Practical Session 3.
Computer Architecture and Assembly Language
Practical Session 5.
Computer Architecture and Assembly Language
Assembly language.
Computer Architecture and Assembly Language
High-Level Language Interface
Computer Architecture and Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Assembly Language Programming II: C Compiler Calling Sequences
Practical Session 4.
Multi-modules programming
Low-Level Thread Dispatching on the x86
System Calls System calls are the user API to the OS
ICS51 Introductory Computer Organization
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Practical Session 5

Addressing Modes

#include #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int DotProduct(int V1[VECTOR_SIZE], int V2[VECTOR_SIZE],int size); extern void MatrixVectorProduct (int M[MATRIX_ROWS * MATRIX_COLUMNS], int V[MATRIX_COLUMNS], int rows, int columns, int result[MATRIX_ROWS]);

int main () } int V1[VECTOR_SIZE] = {1,0,1,0,2}; int V2[VECTOR_SIZE] = {1,0,1,0,-2}; int result = DotProduct(V1,V2,VECTOR_SIZE); printf ("Dot product result: %d\n",result); // Vector: // { } int V[MATRIX_COLUMNS] = {20,1,15}; // Matrix: // { 3 1 3} // {10 2 3} int M[MATRIX_ROWS * MATRIX_COLUMNS] = {3,1,3,10,2,3}; int result_vector[MATRIX_ROWS] = {0,0}; MatrixVectorProduct(M,V,MATRIX_ROWS,MATRIX_COLUMNS,result_vector); printf ("Matrix product result:"); int i; for (i = 0;i < MATRIX_ROWS;i = i + 1) printf (" %d",result_vector[i]); printf ("\n"); return 0; {

DotProduct: push ebp movebp, esp pushebx pushecx pushedx movecx, 0 dot_start: movedx, 0 cmpecx, dword [ebp+16] jedot_end movebx, dword [ebp+8] moveax, dword [ebx + (4*ecx)] movebx, dword [ebp+12] imuldword [ebx + (4*ecx)] adddword [result], eax incecx jmpdot_start dot_end: moveax, dword [result] ; Return Value popedx popecx popebx movesp, ebp popebp ret section.data result: dd 0 section.text globalDotProduct globalMatrixVectorProduct:

MatrixVectorProduct: push ebp movebp, esp pushad movesi, dword [ebp+24] movebx, 0 mv_row: cmpebx, dword [ebp+16] ; Rows jemv_end movecx, 0 mv_column: cmpecx, dword [ebp+20] ; Columns jemv_column_end moveax, dword [ebp+20] ; MATRIX_COLUMNS mulebx movedi, eax; row * MATRIX_COLUMNS movedx, dword [ebp+8] ; M addedi, ecx ; (row * MATRIX_COLUMNS)+column moveax, dword [edx + edi*4] ; M[row*(MATRIX_COLUMNS) + column] movedx, dword [ebp+12] muldword [edx + ecx*4] ; V[column] adddword [esi], eax incecx jmpmv_column mv_column_end: incebx addesi, 4 jmpmv_row mv_end: popad movesp, ebp popebp ret

Linux System Calls

 System calls are low level functions the operating system makes available to applications via a defined API (Application Programming Interface)  System calls represent the interface the kernel presents to user applications.  In Linux all low-level I/O is done by reading and writing file handles, regardless of what particual peripheral device is being accessed - a tape, a socket, even your terminal, they are all files  Low level I/O is performaned by making system calls

 A system call is explicit request to the kernel made via a software interrupt.  The interrupt call ‘0x80’ call to a system call handles.  To perform Linux system calls we have to do following:  Put the system call number in EAX register.  Set up the arguments to the system call in EBX,ECX, etc.  call the relevant interrupt (for DOS, 21h; for Linux, 80h).  The result is usually returned in EAX. Anatomy of System Calls

 There are six registers that are used for the arguments that the system call takes. The first argument goes in EBX, the second in ECX, then EDX, ESI, EDI, and finally EBP. If more then 6 arguments needed (not likely), the EBX register must contain the memory location where the list of arguments is stored.  Files (in Linux everything is a file) are referenced by an integer file descriptor.

sys_write – write into a file system call number (in EAX): 4 arguments: –EBX: The file descriptor. –ECX: Pointer to the first byte to read (beginning of the string). –EDX: Number of bytes (characters) to write. Returns in EAX: Number of bytes written. On errors: -1.

sys_open - open a file system call number (in EAX): 5 arguments: –EBX: The pathname of the file to open/create –ECX: set file access bits (can be OR’d togather): O_RDONLY open for reading only O_WRONLY open for writing only O_RDRW open for both reading and writing O_APPEND open for appending to the end of file O_TRUNC truncate to 0 length if file exists O_CREAT create the file if it doesn’t exist –EDX: set file permissions (in case of create). Returns in EAX: file descriptor. On errors: -1.

sys_open - open a file – an example section.data filename: db “file.ext", 0 Handle dd 0 section.text my_func: moveax,5 movecx, 1 ; O_WRONLY mov ebx,filename int 0x80 inc eax ; eax = -1 on error jz error dec eax mov [Handle],eax jmp file_opened file_opened : …. ret error:… ret

 In Linux, there is a collection of manual pages specifically for system calls.  The collection number is 2.  In order to read about a specific system call, use the ‘Man’ command  Example: > man 2 open This will show the manual pages for the ‘open’ system call. The Man 2 Pages

Assigment #2 Writing a simple calculator for unlimited-precision integers. Operators: –Addition (+) –Pop-and-print (p) –Duplicate (d) –2 Power x (^) –Xor (x) –Octal (o) –Hexadecimal (h) –Quit (q) The calculator uses Reverse Polish Notation (RPN semantics) Operands in the input and output will be in hexadecimal or Octal - according to the setting (o/h)

Assigment #2 With RPN every operator follows all of its operands, For example:  ^  2^ x  4 xor (1+2) Operands in the calculator are implicit – taken from a stack The stack data type is implemented by you

Assignment #2 >>calc: 09 >>calc: 1 Stack >>calc: d 01 >>calc: p >>01 >>calc: + 0A >>calc: d >>calc: p 0A >>0A TOS ->

Assignment #2 >>calc: 06 >>calc: xor Stack 0A 06 >>calc: o 0C >>calc: p >>calc: ^ Error: Not Enough Arguments in Stack TOS -> >> 14

Assignment #2 Your program should be able to handle an unbounded operand size. This type could be implemented as a linked list: –Each element represents 2 hexadecimal digits in the number. –An element block is composed of a 4 byte pointer to the next element, and a byte data. For example, the number 0x7D12AF could be represented by the list: Using this representation, each stack element is simply a list head pointer. AF 12 7D0

Assignment #2 The calculator should get and print only unsigned numbers Addition (+) is defined on unsigned operands; unlimited of length. 2 base Power (^) is defined on unsigned operand; no negative power. Any error in operands (amount and size) must be handled by: –Printing an error –Calculator’s stack must remain unchanged. The application must never crash! Throw error messages and ignore bad operations when needed.

Assignment #2 Note that Addition (+) requires adding the least-significant side of operands to easily calculate carry. It will be possible to apply (+) or (xor) on different size operands. You should add leading zeros. No leading zeros will print when you ask to apply (p) action. The implementation is up to the programmer; These are merely suggestions.