Practical Session 5.

Slides:



Advertisements
Similar presentations
COMP 2003: Assembly Language and Digital Logic
Advertisements

Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
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],
PC hardware and x86 3/3/08 Frans Kaashoek MIT
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.
Position Independent Code self sufficiency of combining program.
Practical Session 8 Computer Architecture and Assembly Language.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
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, _,
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
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],
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
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.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
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.
X86 Programming Memory Accessing Modes, Characters, and Strings Computer Architecture.
Practical Session 5 Computer Architecture and Assembly Language.
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
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.
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.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
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.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
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.
Practical Session 3.
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Assembly language.
Format of Assembly language
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Computer Architecture and Assembly Language
Week 12 - Wednesday CS222.
Writing a Useful Program With NASM
High-Level Language Interface
Computer Architecture and Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Computer Architecture and Assembly Language
Assembly Language Programming II: C Compiler Calling Sequences
Practical Session 4.
Multi-modules programming
Computer Architecture and System Programming Laboratory
ICS51 Introductory Computer Organization
Computer Architecture and System Programming Laboratory
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Week 12 - Wednesday CS222.
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 x86 provides a flexible scheme for computing and referring to memory addresses. x86 addressing mode rule : up to two of the 3-bit registers and a 32-bit signed constant can be added together to compute a memory address. One of the registers can be optionally pre-multiplied by 2, 4, or 8. Example of right usage mov dword [myArray + ebx*4+eax], ecx Examples of wrong usage

Addressing Modes Register Addressing operate on a variable or intermediate variable inc eax Immediate Addressing operate on a CONSTANT add ax, 0x4501 Absolute (Direct) Addressing specify the address as a number or label inc word [myString] inc word [0x1000] Register Indirect Addressing inc byte [ebx] Displacement Addressing Effective Address=[register]+displacement inc byte [ebx+0x10] Relative Addressing Effective Address =[PC]+ displacement jnz next ; ≡ jnz $+4 inc eax next: neg eax Indexed Addressing (with Displacement) useful for array indices inc dword [ebx*4] inc dword [ebx*4+eax] inc dword [myArray + ebx*4] inc dword [myArray + ebx*4+eax]

Addressing Modes - Example #include <stdio.h> #define VECTOR_SIZE 5 extern long long* DotProduct (int V1[VECTOR_SIZE], int V2[VECTOR_SIZE], int size); int main () } int V1[VECTOR_SIZE] = {1,0,1,0,2}; int V2[VECTOR_SIZE] = {1,0,1,0,-2}; long long* result = DotProduct(V1,V2,VECTOR_SIZE); printf ("Dot product result: %#llx \n ", result); return 0; {

stack next element of vector V1 next element of vector V2 DotProduct: section .data result: dd 0,0 section .text global DotProduct DotProduct: push ebp mov ebp, esp push ebx push ecx push edx mov ecx, 0 DotProduct_start: mov edx, 0 cmp ecx, dword [ebp+16] je DotProduct_end mov ebx, dword [ebp+8] mov eax, dword [ebx + (4*ecx)] mov ebx, dword [ebp+12] imul dword [ebx + (4*ecx)] add dword [result], eax adc dword [result+4], edx inc ecx jmp DotProduct_start DotProduct_end: mov eax, result ; return value pop edx pop ecx pop ebx mov esp, ebp pop ebp ret int V1[VECTOR_SIZE] = {1,0,1,0,2}; int V2[VECTOR_SIZE] = {1,0,1,0,-2}; long long* result = DotProduct(V1,V2,VECTOR_SIZE); stack VECTOR_SIZE V2 V1 return address EBP previous value old ebx old ecx old edx EBP + 16 EBP + 12 EBP + 8 EBP ESP next element of vector V1 next element of vector V2

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 particular peripheral device is being accessed - a tape, a socket, even your terminal, they are all files. Files are referenced by an integer file descriptor. Low level I/O is performed by making system calls

Anatomy of 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. 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. call the relevant interrupt (for Linux it is 0x80) The result is usually returned in EAX

sys_open - open a file system call number (in EAX): 5 arguments: EBX: pathname of the file to open/create ECX: set file access bits (can be bitwise OR’ed together) 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; can be bitwise OR’ed together) S_IRWXU 0000700 ; RWX mask for owner S_IRUSR 0000400 ; R(read) for owner USR(user) S_IWUSR 0000200 ; W(write) for owner S_IXUSR 0000100 ; X(execute) for owner returns (in EAX): file descriptor. On errors: -1. section .data fileName: db “file.txt", 0 handle dd 0 section .text global _start _start: file_open: mov eax, 5 ; system call number (sys_open) mov ecx, 1 ; set file access bits (O_WRONLY) mov ebx, fileName ; set file name mov edx, S_IRUSR ; set file permissions int 0x80 ;call kernel mov [handle], eax ; move file handle to memory mov eax, 1 ;system call number (sys_exit) mov ebx, 0 ; exit status int 0x80 ;call kernel

sys_read – read into a file system call number (in EAX): 3 arguments: EBX: file descriptor ECX: pointer to input buffer EDX: maximal number of bytes to receive (buffer size) returns (in EAX): number of bytes received On errors: -1or 0 (no bits read) section .bss buffer: resb 1 section .text global _start _start: mov eax, 3 ; system call number (sys_read) mov ebx, 0 ; file descriptor (stdin) mov ecx, buffer ; buffer to put the read data mov edx, 1 ; read byte count int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) mov ebx, 0 ; exit status int 0x80 ;call kernel

sys_write – write into a file system call number (in EAX): 4 arguments: EBX: 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 section .data msg db ‘Message',0xa ;our string len equ $ - msg ;length of our string section .text global _start _start: mov ebx,1 ;file descriptor (stdout) mov ecx, msg ;message to write mov edx, len ;message length mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) mov ebx, 0 ; exit status

sys_lseek – change a file pointer system call number (in EAX): 19 arguments: EBX: file descriptor ECX: offset (number of bytes to move) EDX: where to move from SEEK_SET 0 ; beginning of the file SEEK_CUR 1 ; current position of the file pointer SEEK_END 2 ; end of file returns (in EAX): Current position of the file pointer On errors: SEEK_SET section .data fileName: db “file.txt", 0 handle dd 0 section .text global _start _start: file_open: mov eax, 5 ; system call number (sys_open) mov ecx, 1 ; set file access bits (O_WRONLY) mov ebx, fileName ; set file name mov edx, S_IRUSR ; set file permissions int 0x80 ;call kernel mov [handle], eax ; move file handle to memory mov ecx,15 ; number of byte to move mov edx,0 ; move from beginning of the file mov eax,19 ; the number of the syscall 'lseek‘ mov eax, 1 ;system call number (sys_exit) mov ebx, 0 ; exit status int 0x80 ;call kernel

sys_close – close a file system call number (in EAX): 6 arguments: EBX: file descriptor returns (in EAX): nothing meaningful On errors: -1 section .data fileName: db “file.txt", 0 handle dd 0 section .text global _start _start: file_open: mov eax, 5 ; system call number (sys_open) mov ecx, 1 ; set file access bits (O_WRONLY) mov ebx, fileName ; set file name mov edx, S_IRUSR ; set file permissions int 0x80 ;call kernel mov [handle], eax ; move file handle to memory mov ebx, [handle] mov eax, 6 int 0x80 mov eax, 1 ;system call number (sys_exit) mov ebx, 0 ; exit status int 0x80 ;call kernel

Linux System Calls - Example section .data fileName: db “file.txt", 0 handle dd 0 section .bss buffer: resb 1 section .text global _start _exit: mov ebx, [handle] mov eax, 6 ; system call (sys_close) int 0x80 ; call kernel mov eax, 1 ; system call (sys_exit) mov ebx, 0 ; exit status int 0x80 ; call kernel _start: mov eax, 5 ; system call (sys_open) mov ecx, 1 ; set file access bits (O_WRONLY) mov ebx, fileName ; set file name mov edx, S_IRUSR ; set file permissions int 0x80 ; call kernel mov [handle], eax ; move file handle to memory mov eax, 3 ; system call (sys_read) mov ebx, [handle] ; file handle mov ecx, buffer ; buffer mov edx, 1 ; read byte count int 0x80 ; call kernel cmp eax, 0 je _exit mov eax, 4 ; system call (sys_write) mov ebx, 1 ; stdout mov edx, ; write byte count jmp _start

Assignment #2 Writing a simple calculator for unlimited-precision integers. Operators: Addition unsigned (+) Pop-and-print (p) Duplicate (d) Log 2 (l) Number of '1' bits (n) Quit (q) Operands in the input and output will be in hexadecimal

Assignment #2 The calculator uses Reverse Polish Notation (RPN) i.e. every operator follows all of its operands Example: 1 2 +  1+2 10 l  log2 (10) (indeed results with log2(8)) 4 n  number of ‘1’ bits in 4 Operands in the calculator are implicit – taken from a stack The stack data type is implemented by you

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

Assignment #2 Stack >>calc: n TOS -> >>calc: p 0A 02 >> 2 >>calc: + Error: Not Enough Arguments in Stack

Assignment #2 Your program should be able to handle an unbounded operand size. This may 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. Example: 0x7D12AF could be represented by the following linked list: Using this representation, each stack element is simply a list head pointer. AF 12 7D

Assignment #2 Declare a label "main:" and "global main" in your assembly program. Declare "extern printf, extern malloc" and "extern gets" so you will be able to use those functions in the program  Compile and link your assembly file calc.s as follows: nasm -f elf calc.s -o calc.o gcc -m32 -Wall -g calc.o -o calc.bin Note: there is no need in c file! Gcc will “connect” external c functions to your assembly program.