70 likes | 155 Views
This review session delves into the Little-Endian memory structure, explaining what's stored in memory sections like .rodata, .bss, and .text. It includes practical examples of calculating and printing Fibonacci numbers using arrays and recursion in C language.
E N D
Practical session 7 review
Little – Endian • What’s in memory? Section .rodata a: DB ‘hello’, 0x20, ’world’, 10, 0 b: DW ‘hello’, 0x20, ’world’, 10, 0 c: DD ‘h’, ’e’, ‘l’, ‘l’, ‘o’
Little – Endian • What’s in memory? Section .bss a: RESD 1 b: RESB 12 Section .text … moveax, 0x99887766 mov [a],eax … movdword[b], ‘xyz’ movebx,[b]
gets a number n>0 as argument, calculate & print fib[0..n-1] arrays - example #include <stdio.h> extern void fib(intn,int* arr); int main(intargc,char* argv[]){ //gets a number 'n' from input, calculate & print fib[1..n] int n= atoi(argv[1]); inti=0; intans[n]; fib(n,ans); for(i=0; i< n; i++) printf("%d ",ans[i]); printf("\n"); return 0; }
arrays- example global fib section .text fib: push ebp movebp,esp pusha ;intarr[] movesi,[ebp+12] ;int n is assumed to be > 1 movecx,[ebp+8] movdword[esi],0 ;fib(0) = 0 movdword[esi+4],1 ;fib(1) = 1 sub ecx,2 jz end l: moveax,[esi] add eax,[esi+4] mov [esi+8],eax add esi,4 loop l end: popa pop ebp ret
gets a number n>=0 as argument, calculate & print fib(n) recursively Recursion - example #include <stdio.h> extern intfibReg(int n); int main(intargc,char* argv[]){ int n= atoi(argv[1]); intans= fibReg(n); printf("%d \n",ans); return 0; }
global fibReg extern printf section .text str: db "n= %d; ans= %d",10,0 fibReg: push ebp movebp,esp push ebx push ecx cmpdword[ebp+8],1 jl zero ;if(n==0) je one ;if(n==1) ;;get fibReg(n-2) movebx,[ebp+8] sub ebx,2 push ebx call fibReg add esp,4 movecx,eax Recursion - example ;;get fibReg(n-1) incebx push ebx call fibReg add esp,4 add eax,ecx jmp end zero: mov eax,0 jmp end one: mov eax,1 end: pop ecx pop ebx movesp,ebp pop ebp ret