40 likes | 141 Views
Parameter Passing Exercises. Exercise 1:. @foo$ric: ldc ‘a’ subr 2 jnz else ldr 2 ret else: ldr 1 ret. Convert the following C++ code to H1 assembler. int foo(int a, char ch = ‘a’) { if ch == ‘a’ return (int)ch;
E N D
Parameter Passing Exercises Assembler
Exercise 1: @foo$ric: ldc ‘a’ subr 2 jnz else ldr 2 ret else: ldr 1 ret • Convert the followingC++ code to H1assembler. int foo(int a, char ch = ‘a’) { if ch == ‘a’ return (int)ch; else return a; . . . int x = 5; int y; y = foo(x); x: dw 5 y: dw 0 ldc ‘a’ push ld x push call @foo$ric dloc 2 st y x: 5 y: 0 ret addr a: 5 ch: ‘a’ Assembler
Exercise 2: @foo$ipi: ldc 5 push addr 2 push ldr 4 sti ldi dloc 1 ret • Convert the followingC++ code to H1assembler. int foo(int a, int*p) { int b = 5; *p = a + b; return *p . . . int x = 5; int y; x = foo(x,&y); x: dw 5 y: dw 0 ldc y push ld x push call @foo$ipi dloc 2 st xZZ x: 5 y: 0 heap b: 5 ret addr a: 5 p: Assembler
Exercise 3: @foo$ripi: aloc 1 ldr 2 ; b = a ldi str 0 ldr 3 ; a = *p ldi push ldr 3 sti ldr 0 ; *p = b push ldr 4 sti dloc 1 ret • Convert the followingC++ code to H1assembler. void foo(int& a, int*p) { int b; b = a; a = *p; *p = b; return; . . . int x = 5; int y = 6; foo(x,&y); x: 5 y: 6 x: dw 5 y: dw 6 main: ldc y push ldc x push call @foo$ripi dloc 2 heap b: - ret addr a: p: Assembler