240 likes | 432 Views
IKI10230 Pengantar Organisasi Komputer Kuliah no. 07: CALL, RET, Stacks. Sumber : 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization , ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB 4. Intel Architecture Software Developer’s Manual. 31 Maret 2004
E N D
IKI10230Pengantar Organisasi KomputerKuliah no. 07: CALL, RET, Stacks Sumber:1. Paul Carter, PC Assembly Language2. Hamacher. Computer Organization, ed-53. Materi kuliah CS61C/2000 & CS152/1997, UCB4. Intel Architecture Software Developer’s Manual 31 Maret 2004 L. Yohanes Stefanus (yohanes@cs.ui.ac.id)Bobby Nazief (nazief@cs.ui.ac.id) bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/
Unconditional vs Conditional (1/2) • Unconditional Branch (JMP) umumnya digunakan untuk melengkapi Conditional Branch (Jcc) untuk “keluar” dari IF-THEN-ELSE-BLOCK: • ; code to set flags • Jcc Then-Block • ; Else-Block instructions • Jmp Next-Block • Then-Block: • ; Then-Block instructions • Next-Block: • ; Next-block instructions
Unconditional vs Conditional (2/2) • the following pseudo-code: if ( EAX == 0 ) EBX = 1; else EBX = 2; • could be written in assembly as: • cmp eax, 0 ; set flags (ZF set if eax - 0 = 0) • jz thenblock; if ZF is set branch to thenblock • mov ebx, 2 ; ELSE part of IF • jmp next; jump over THEN part of IF • thenblock: • mov ebx, 1 ; THEN part of IF • next:
Complexity of Conditional Branches • Simple Conditional Branches: only look at 1 flag • JZJump if ZF is set (result is 0) • JNZJump if ZF is unset • JCJump if CF is set (carry-out is generated) • JNCJump if CF is unset • JOJump if OF is set (result is overflow) • JNOJump if OF is unset • JSJump if SF is set (result is negative) • JNSJump if SF is unset • JPJump if PF is set (parity is even) • JNPJump if PF is unset • Complex (higher level) Conditional Branches: • Unsigned Numbers: • JA/JNBE Jump if above/Jump if not below or equal(CF or ZF) == 0 • JAE/JNB Jump if above or equal/Jump if not below CF == 0 • JB/JNAE Jump if below/Jump if not above or equal CF == 1 • JBE/JNA Jump if below or equal/Jump if not above(CF or ZF) == 1 • JE Jump if equal ZF == 1 • Signed Numbers: • JG/JNLE Jump if greater/Jump if not less or equal((SF xor OF) or ZF) == 0 • JGE/JNL Jump if greater or equal/Jump if not less(SF xor OF) == 0 • JL/JNGE Jump if less/Jump if not greater or equal(SF xor OF) == 1 • JLE/JNG Jump if less or equal/Jump if not greater((SF xor OF) or ZF) == 1
Complex Condition (v1: using simple ones) • consider the following pseudo-code: if ( EAX >= 5 ) EBX = 1; else EBX = 2; • here is assembly code that tests for these conditions(assuming that EAX is signed): • cmp eax, 5 • js signon ; goto signon if SF = 1 • jo elseblock ; goto elseblock if OF = 1 and SF = 0 • jmp thenblock ; goto thenblock if SF = 0 and OF = 0 • signon: • jo thenblock ; goto thenblock if SF = 1 and OF = 1 • elseblock: ; SF=0 and OF=1 or SF=1 and OF=0 • mov ebx, 2 • jmp next • thenblock: • mov ebx, 1 • next:
CMP V1,V2 ; v1,v2: signed numbers • JGE Jump if greater or equal (SF xor OF) == 0 • Remember: overflow may occur during comparison! • v1>0 & v2>0 or v1<0 & v2<0 • v1>v2 v1-v2>0 SF = 0 & OF = 0 • v1=v2 v1-v2=0 SF = 0 & OF = 0 • v1<v2 v1-v2<0 SF = 1 & OF = 0 • v1>0 & v2<0, assume 4-bit integers • v1=6>v2=-1 v1-v2=7>0 SF = 0 & OF = 0 • v1=7>v2=-3 v1-v2=-6>0 SF = 1 & OF = 1 • v1<0 & v2>0, assume 4-bit integers • v1=-1<v2=6 v1-v2=-7<0 SF = 1 & OF = 0 • v1=-3<v2=7 v1-v2=6<0 SF = 0 & OF = 1
CMP V1,V2 ; v1,v2: unsigned numbers • JAJump if above (CF or ZF) == 0 • JAEJump if above or equal CF == 0 • JBJump if below CF == 1 • JBEJump if below or equal (CF or ZF) == 1 • v1>v2 v1-v2>0 CF = 0 & ZF = 0 • v1=v2 v1-v2=0 CF = 0 & ZF = 1 • v1<v2 v1-v2<0 CF = 1 & ZF = 0
Complex Condition (v2: using complex ones) • consider the following pseudo-code: if ( EAX >= 5 ) EBX = 1; else EBX = 2; • here is assembly code that tests for these conditions(assuming that EAX is signed): • cmp eax, 5 • jge thenblock • mov ebx, 2 • jmp next • thenblock: • mov ebx, 1 • next:
Loop (1) • thefollowing pseudo-code: sum = 0; for ( i=10; i>0; i-- ) sum += i; • could be translated into assembly as: • mov eax, 0; eax is sum • mov ecx, 10; ecx is i • loop_start: • add eax, ecx • loop loop_start • or: • mov eax, 0; eax is sum • mov ecx, 10; ecx is i • loop_start: • add eax, ecx • dec ecx • jnz loop_start
Loop (2) • thefollowing pseudo-code: sum = 0; for ( i=0; i<10; i++ ) sum += i; • could be translated into assembly as: • mov eax, 0; eax is sum • mov ecx, 0; ecx is i • loop_start: • add eax, ecx • inc ecx • cmp ecx,10 • jne loop_start
WHILE Loop • The while loop is a top tested loop: while( EAX == 1 ) { body of loop; } • This could be translated into: • while: • cmp eax,1 • jne endwhile ; select xx so that branches if false • ; body of loop • jmp while • endwhile:
DO WHILE Loop • The do while loop is a bottom tested loop: do { body of loop; } while( EAX == 1 ); • This could be translated into: • do: • ; body of loop • cmp eax,1 • jedo ; select xx so that branches if true
Stacks • Lokasi memori yang pengaksesan datanya dibatasi dengan cara LIFO (Last In, First Out) data terakhir yang disimpan kedalam stack akan menjadi data pertama yang diperoleh pada saat stack diakses • Push: memasukkan data ke Stack • Pop: mengeluarkan data yang berada di top-of-stack (TOS) alamat TOS disimpan dalam register Stack Pointer (ESP) 0 Push Top-of-Stack: ESP Stack Bottom-of-Stack Pop 2k-1
Operasi pada Stacks Push NewItem: Sub ESP,4 Mov [ESP],NewItem Pop Item: Mov Item,[ESP] Add ESP,4 SP 19 SP -28 17 NewItem SP 19 -28 Item SP 17 Bahaya Overflow NewItem 19 Item -28 Bahaya Underflow
Call & Ret Instructions • The CALL instruction transfers program control from the current (or calling procedure) toanother procedure (the called procedure). • To allow a subsequent return to the calling procedure,the CALL instruction saves the current contents of the EIP register on the stack before jumpingto the called procedure. • The EIP register (prior to transferring program control) contains theaddress of the instruction following the CALL instruction. • When this address is pushed on thestack, it is referred to as the return instruction pointer or return address. • The RET instruction transfers program control from the procedure currently being executed (thecalled procedure) back to the procedure that called it (the calling procedure). • Transfer of controlis accomplished by copying the return instruction pointer from the stack into the EIP register. • The RET instruction has an optional operand, the value of which is added to the contents of theESP register as part of the return operation. This operand allows the stack pointer to be incrementedto remove parameters from the stack that were pushed on the stack by the callingprocedure.
SubRoutines (the Called Procedure) • SubRoutine adalah sekumpulan instruksi yang mengerjakan suatu fungsi tertentu dan diakhiri dengan instruksi RET (return) • SubRoutine biasanya dipanggil (CALL) oleh program lain dan setelah SubRoutine selesai mengerjakan fungsinya, kendali program dikembalikan (RET) ke program pemanggil LokasiSubroutine SUB 1000 instruksi_i . . . Ret LokasiProgram Utama 200 Call SUB 201 instruksi_berikutnya 1000 EIP 201 [ESP] 201
Setelah ‘Call SUB’ Setelah ‘Return’ ESP 202 202 ESP 99 99 EIP EIP 202 1000 EAX EAX 99 SubRoutine’s Call & Stacks LokasiSubroutine SUB 1000 instruksi_i . . . Add EAX,EBX . . . Return LokasiProgram Utama • Push EAX • Call SUB • Pop EAX • instruksi_berikutnya
Passing Parameters via Registers Contoh: ... int sum; int n = 112; int[ ] num = new int[n]; … sum = ListAdd(n, num); ... • Caller & Subroutine share the same registers • Passing by Value: [N] via ECX • Passing by Reference: NUM via EBX LokasiSubroutine LISTADD LISTADD: Mov EAX,0 L1: Add EAX,[EBX] Add EBX,4 Loop L1 Ret LokasiProgram Utama • Mov ECX,[N] • Mov EBX,NUM • Call LISTADD • Mov EAX,[SUM] …
ECX EBX EAX EIP (prg. Utama) [N] NUM Passing Parameters via Stack Frame LokasiSubroutine LISTADD LISTADD:Push EAX Push EBX Push ECX Mov ECX,[ESP+16] Mov EBX,[ESP+20] Mov EAX,0 L1: Add EAX,[EBX] Add EBX,4 Loop L1 Mov [ESP+16],EAX Pop ECX Pop EBX Pop EAX Ret LokasiProgram Utama Push NUM Push [N] Call LISTADD Mov EAX,[ESP] Mov [SUM],EAX Add ESP,8 … ESPESP+4ESP+8ESP+12ESP+16ESP+20 ESPESP+4
sub1.asm (SubRoutine Call w/o CALL-RET) mov ebx, input1 ; store address of input1 into ebx mov ecx, ret1 ; store return address into ecx jmp short get_int ; read integer ret1: ... ; subprogram get_int ; Parameters: ; ebx - address of dword to store integer into ; ecx - address of instruction to return to ; Notes: ; value of eax is destroyed get_int: call read_int mov [ebx], eax ; store input into memory jmp ecx ; jump back to caller
sub2.asm (Passing Parameters via Registers) mov ebx, input1 ; store address of input1 into ebx call get_int ; read integer ... ; subprogram get_int ; Parameters: ; ebx - address of word to store integer into ; Notes: ; value of eax is destroyed get_int: call read_int mov [ebx], eax ; store input into memory ret ; jump back to caller
sub3.asm (Passing Parameters via Stack Frame) push edx ; save i on stack push dword input ; push address on input on stack call get_int add esp, 8 ; remove i and &input from stack; subprogram get_int ... ; Parameters (in order pushed on stack) ; number of input (at [ebp + 12]) ; address of word to store input into (at [ebp + 8]) ; Notes: ; values of eax and ebx are destroyed get_int: push ebp mov ebp, esp mov eax, [ebp + 12] call print_int ... call read_int mov ebx, [ebp + 8] mov [ebx], eax; store input into memory pop ebp ret ; jump back to caller