1 / 17

Practical Session 5

Practical Session 5. Addressing Modes. #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],

posborne
Download Presentation

Practical Session 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Practical Session 5

  2. Addressing Modes

  3. #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]);

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

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

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

  7. Linux System Calls

  8. 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

  9. Anatomy of System Calls • 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.

  10. 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.

  11. 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.

  12. ; This program demonstrates basic text output to a screen. ; No "C" library functions are used. ; Calls are made to the operating system directly. (int 80 hex) ; hello.asm a first program for nasm for Linux, Intel, gcc ; assemble: nasm -f elf -l hello.lst hello.asm ; link: ld -m elf_i386 -o hello hello.o ; run: hello ; output is: Foo Bar section .rodata ; data section msg: db "Foo Bar",10 ; the string to print, 10=cr len: equ $-msg ; "$" means "here" ; len is a value, not an address section .text ; code section global _start ; make label available to linker _start: ; standard linker entry point mov ebx,1 ; arg1, where to write: screen (file descriptor = 1) mov ecx,msg ; arg2, pointer to string mov edx,len ; arg3, length of string to print mov eax,4 ; write system call procedure number int 0x80 ; interrupt 80 hex, call kernel mov ebx,0 ; exit code, 0=normal mov eax,1 ; exit command to kernel int 0x80 ; interrupt 80 hex, call kernel

  13. 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 (0) open for reading only • O_WRONLY (1) open for writing only • O_RDRW (2) open for both reading and writing • O_CREAT (0100) create the file if it doesn’t exist • O_TRUNC (01000) truncate to 0 length if file exists • O_APPEND (02000) open for appending to the end of file • EDX: set file permissions (in case of create). • Returns in EAX: file descriptor. • On errors: -1.

  14. section .data filename: db “file.ext", 0 Handle dd 0 section .text my_func: mov eax,5 mov ecx, 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 sys_open - open a file – an example

  15. ; This program demonstrates basic text output to a FILE. section .rodata msg: db “Foo Bar",10 ; the string to print len1: equ $-msg error_msg: db "Error",10 len2: equ $-error_msg ; error message when the operation fails filename: db "hello.txt",0 ; file name section .text global _start _start: ; first, create (open) a file and get a descriptor mov eax, 5 ; open system call procedure number mov ebx, filename ; file name pointer mov ecx, 2 | 0q0100 ; (O_RDRW | O_O_CREATE) create, read-write access mode mov edx, 0q777 ; full permission int 0x80 ; eax has the file descriptor to the file, or negative integer if error occured cmp eax, 0 jl error_print mov ebx,eax ; arg1, where to write: file descriptor of hello.txt mov eax, 4 mov ecx,msg ; arg2, pointer to a string message mov edx,len1 ; arg3, length of string to print int 0x80 ; interrupt 80 hex, call kernel …

  16. Cont’ mov ebx, 0 ; exit code, 0=normal exit: mov eax,1 ; exit command to kernel int 0x80 ; interrupt 80 hex, call kernel error_print: mov ebx,1 ; print the error message mov ecx, error_msg mov edx,len2 mov eax,4 int 0x80 mov ebx,1 jmp exit ; exit code, 1=error

  17. The Man 2 Pages • 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.

More Related