Practical Session 5 Addressing Modes include stdio h

  • Slides: 19
Download presentation
Practical Session 5

Practical Session 5

Addressing Modes

Addressing Modes

#include <stdio. h> #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int

#include <stdio. h> #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int Dot. Product(int V 1[VECTOR_SIZE], int V 2[VECTOR_SIZE], int size); extern void Matrix. Vector. Product (int M[MATRIX_ROWS * MATRIX_COLUMNS], int V[MATRIX_COLUMNS], int rows, int columns, int result[MATRIX_ROWS]);

int main () { int V 1[VECTOR_SIZE] = {1, 0, 2}; int V 2[VECTOR_SIZE]

int main () { int V 1[VECTOR_SIZE] = {1, 0, 2}; int V 2[VECTOR_SIZE] = {1, 0, -2}; int result = Dot. Product(V 1, V 2, VECTOR_SIZE); printf ("Dot product result: %dn", result); // Vector: // {20 1 15} 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}; Matrix. Vector. Product(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; }

section. data result: dd 0 section. text global Dot. Product Matrix. Vector. Product: Dot.

section. data result: dd 0 section. text global Dot. Product Matrix. Vector. Product: Dot. Product: push mov push ebp, esp ebx ecx edx mov ecx, 0 mov cmp je mov mov imul add edx, 0 ecx, dword [ebp+16] dot_end ebx, dword [ebp+8] eax, dword [ebx + (4*ecx)] ebx, dword [ebp+12] dword [ebx + (4*ecx)] dword [result], eax inc jmp ecx dot_start mov pop pop mov pop ret eax, dword [result] ; Return Value edx ecx ebx esp, ebp dot_start: dot_end:

Matrix. Vector. Product: push ebp mov ebp, esp pushad mov esi, dword [ebp+24] ebx,

Matrix. Vector. Product: push ebp mov ebp, esp pushad mov esi, dword [ebp+24] ebx, 0 cmp je ebx, dword [ebp+16] ; Rows mv_end mv_column_end: inc add jmp mv_end: popad mov pop ret mv_row: mov mv_column: cmp je ebx esi, 4 mv_row esp, ebp ecx, 0 ecx, dword [ebp+20] ; Columns mv_column_end mov mul mov add mov mul add eax, dword [ebp+20] ebx edi, eax edx, dword [ebp+8] edi, ecx eax, dword [edx + edi*4] edx, dword [ebp+12] dword [edx + ecx*4] dword [esi], eax inc jmp ecx mv_column ; MATRIX_COLUMNS ; row * MATRIX_COLUMNS ; M ; (row * MATRIX_COLUMNS)+column ; M[row*(MATRIX_COLUMNS) + column] ; V[column]

Linux System Calls

Linux System Calls

Linux System Calls q System calls are low level functions the operating system makes

Linux System Calls q System calls are low level functions the operating system makes available to applications via a defined API (Application Programming Interface) q System calls represent the interface the kernel presents to user applications. q 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 q Low level I/O is performaned by making system calls

Anatomy of System Calls q A system call is explicit request to the kernel

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

q There are six registers that are used for the arguments that the system

q 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. q 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 •

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:

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",

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

The Man 2 Pages q In Linux, there is a collection of manual pages

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

Assigment #2 • Writing a simple calculator for unlimited-precision integers. • Operators: – –

Assigment #2 • Writing a simple calculator for unlimited-precision integers. • Operators: – – – – Addition (+) Pop-and-print (p) Duplicate (d) Bitwise and (&) Bitwise xor (^) Binary (b) Hexadecimal (h) • The calculator uses Reverse Polish Notation (RPN semantics) • Operands in the input will be in hexadecimal presentation, while output can be either in hexadecimal or binary (according to the setting).

Assigment #2 • With RPN every operator follows all of its operands, • For

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

Assignment #2 >>calc: 0 x 9 >>calc: 0 x 1 TOS -> >>calc: d

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

Assignment #2 >>calc: 0 x 4 TOS -> >>calc: & TOS -> >>calc: p

Assignment #2 >>calc: 0 x 4 TOS -> >>calc: & TOS -> >>calc: p TOS -> >>0 x 00 >>calc: + Error: Not Enough Arguments in Stack 0 x 0 A 0 x 00 0 x 4

Assignment #2 • Your program should be able to handle an unbounded operand size.

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 0 x 7 D 12 AF could be represented by the list: AF 7 D 0 12 • Using this representation, each stack element is simply a list head pointer.