Practical Session 5

21
Practical Session 5

description

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], - PowerPoint PPT Presentation

Transcript of Practical Session 5

Page 1: Practical Session 5

Practical Session 5

Page 2: Practical Session 5

Addressing Modes

Page 3: Practical Session 5

#include <stdio.h>#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]);

Page 4: Practical Session 5

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: // {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};

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;{

Page 5: Practical Session 5

DotProduct:push ebpmov ebp, esppush ebxpush ecxpush edx

mov ecx, 0dot_start:

mov edx, 0cmp ecx, dword [ebp+16]je dot_endmov ebx, dword [ebp+8]mov eax, dword [ebx + (4*ecx)]mov ebx, dword [ebp+12]imul dword [ebx + (4*ecx)]add dword [result], eax

inc ecxjmp dot_start

dot_end:mov eax, dword [result] ; Return Valuepop edxpop ecxpop ebxmov esp, ebppop ebpret

section .dataresult:

dd 0

section .textglobal DotProductglobal MatrixVectorProduct:

Page 6: Practical Session 5

MatrixVectorProduct:push ebpmov ebp, esppushad

mov esi, dword [ebp+24]mov ebx, 0

mv_row:cmp ebx, dword [ebp+16] ; Rowsje mv_end

mov ecx, 0mv_column:

cmp ecx, dword [ebp+20] ; Columnsje mv_column_end

mov eax, dword [ebp+20] ; MATRIX_COLUMNSmul ebx mov edi, eax ; row * MATRIX_COLUMNSmov edx, dword [ebp+8] ; Madd edi, ecx ; (row * MATRIX_COLUMNS)+columnmov eax, dword [edx + edi*4] ; M[row*(MATRIX_COLUMNS) + column]mov edx, dword [ebp+12]mul dword [edx + ecx*4] ; V[column]add dword [esi], eax

inc ecxjmp mv_column

mv_column_end:inc ebxadd esi, 4jmp mv_row

mv_end:popadmov esp, ebppop ebpret

Page 7: Practical Session 5

Linux System Calls

Page 8: Practical Session 5

Linux System CallsLinux 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

Page 9: Practical Session 5

A system call is explicit request to the kernel made via asoftware 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 CallsAnatomy of System Calls

Page 10: Practical Session 5

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.

Page 11: Practical Session 5

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.

Page 12: Practical Session 5

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.

Page 13: Practical Session 5

sys_open - open a file – an examplesection .datafilename: db “file.ext", 0Handle dd 0

section .textmy_func:

mov eax,5

mov ecx, 1 ; O_WRONLYmov ebx,filenameint 0x80inc eax ; eax = -1 on errorjz error dec eaxmov [Handle],eaxjmp file_opened

file_opened : ….

reterror: …

ret

Page 14: Practical Session 5

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 openThis will show the manual pages for the ‘open’ system call.

The Man 2 PagesThe Man 2 Pages

Page 15: Practical Session 5

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)

Page 16: Practical Session 5

Assigment #2• With RPN every operator follows all of

its operands,

• For example:1 2 + 1+2

3 ^ 2^3

4 1 2 + x 4 xor (1+2)

• Operands in the calculator are implicit – taken from a stack

• The stack data type is implemented by you

Page 17: Practical Session 5

Assignment #2

>>calc: 09

>>calc: 1

Stack

09

01>>calc: d 01

>>calc: p

>>01

>>calc: +

0A

>>calc: d

>>calc: p

0A

>>0A

TOS ->

TOS ->

TOS ->

TOS ->

Page 18: Practical Session 5

Assignment #2

>>calc: 06

>>calc: xor

Stack

0A

06>>calc: o

0C

>>calc: p

>>calc: ^

Error: Not Enough Arguments in Stack

TOS ->

TOS ->

TOS ->

>> 14

Page 19: Practical Session 5

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 7D 0

Page 20: Practical Session 5

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.

Page 21: Practical Session 5

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.