AT90S8515 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

AT90S8515

Description:

The RET instruction transfers program control from the procedure currently being ... The RET instruction has an optional operand, the value of which is added to the ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 25
Provided by: bobbyn4
Category:
Tags: at90s8515 | ret

less

Transcript and Presenter's Notes

Title: AT90S8515


1
IKI10230Pengantar Organisasi KomputerKuliah no.
07 CALL, RET, Stacks
Sumber1. Paul Carter, PC Assembly Language2.
Hamacher. Computer Organization, ed-53. Materi
kuliah CS61C/2000 CS152/1997, UCB4. Intel
Architecture Software Developers Manual
31 Maret 2004 L. Yohanes Stefanus
(yohanes_at_cs.ui.ac.id)Bobby Nazief
(nazief_at_cs.ui.ac.id) bahan kuliah
http//www.cs.ui.ac.id/kuliah/POK/
2
  • REVIEW

3
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

4
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

5
Complexity of Conditional Branches
  • Simple Conditional Branches only look at 1 flag
  • JZ Jump if ZF is set (result is 0)
  • JNZ Jump if ZF is unset
  • JC Jump if CF is set (carry-out is generated)
  • JNC Jump if CF is unset
  • JO Jump if OF is set (result is overflow)
  • JNO Jump if OF is unset
  • JS Jump if SF is set (result is negative)
  • JNS Jump if SF is unset
  • JP Jump if PF is set (parity is even)
  • JNP Jump 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

6
Complex Condition (v1 using simple ones)
  • consider the following pseudo-code
  • if ( EAX gt 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 SF0 and OF1 or SF1 and OF0
  • mov ebx, 2
  • jmp next
  • thenblock
  • mov ebx, 1
  • next

7
CMP V1,V2 v1,v2 signed numbers
  • JGE Jump if greater or equal (SF xor OF) 0
  • Remember overflow may occur during comparison!
  • v1gt0 v2gt0 or v1lt0 v2lt0
  • v1gtv2 ? v1-v2gt0 ? SF 0 OF 0
  • v1v2 ? v1-v20 ? SF 0 OF 0
  • v1ltv2 ? v1-v2lt0 ? SF 1 OF 0
  • v1gt0 v2lt0, assume 4-bit integers
  • v16gtv2-1 ? v1-v27gt0 ? SF 0 OF 0
  • v17gtv2-3 ? v1-v2-6gt0 ? SF 1 OF 1
  • v1lt0 v2gt0, assume 4-bit integers
  • v1-1ltv26 ? v1-v2-7lt0 ? SF 1 OF 0
  • v1-3ltv27 ? v1-v26lt0 ? SF 0 OF 1

8
CMP V1,V2 v1,v2 unsigned numbers
  • JA Jump if above (CF or ZF) 0
  • JAE Jump if above or equal CF 0
  • JB Jump if below CF 1
  • JBE Jump if below or equal (CF or ZF) 1
  • v1gtv2 ? v1-v2gt0 ? CF 0 ZF 0
  • v1v2 ? v1-v20 ? CF 0 ZF 1
  • v1ltv2 ? v1-v2lt0 ? CF 1 ZF 0

9
Complex Condition (v2 using complex ones)
  • consider the following pseudo-code
  • if ( EAX gt 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

10
Loop (1)
  • the following pseudo-code
  • sum 0
  • for ( i10 igt0 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

11
Loop (2)
  • the following pseudo-code
  • sum 0
  • for ( i0 ilt10 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

12
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

13
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
  • je do select xx so that branches if true

14
  • Stacks, Subroutine Calls

15
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
16
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
SP
Item
17
Bahaya Overflow
NewItem
19
Item
-28
Bahaya Underflow
17
Call Ret Instructions
  • The CALL instruction transfers program control
    from the current (or calling procedure) to
    another 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
    jumping to the called procedure.
  • The EIP register (prior to transferring program
    control) contains the address of the instruction
    following the CALL instruction.
  • When this address is pushed on the stack, it is
    referred to as the return instruction pointer or
    return address.
  • The RET instruction transfers program control
    from the procedure currently being executed (the
    called procedure) back to the procedure that
    called it (the calling procedure).
  • Transfer of control is 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 the
    ESP register as part of the return operation.
    This operand allows the stack pointer to be
    incremented to remove parameters from the stack
    that were pushed on the stack by the calling
    procedure.

18
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

Lokasi Subroutine SUB 1000 instruksi_i . .
. Ret
Lokasi Program Utama 200 Call SUB 201 instruksi_be
rikutnya
1000
EIP
201
ESP
201
19
SubRoutines Call Stacks
  • Lokasi Program Utama
  • Push EAX
  • Call SUB
  • Pop EAX
  • instruksi_berikutnya

Lokasi Subroutine SUB 1000 instruksi_i . .
. Add EAX,EBX . . . Return
20
Passing Parameters via Registers
  • Contoh
  • ...
  • int sum
  • int n 112
  • int num new intn
  • sum ListAdd(n, num)
  • ...
  • Caller Subroutine share the same registers
  • Passing by Value N via ECX
  • Passing by Reference NUM via EBX
  • Lokasi Program Utama
  • Mov ECX,N
  • Mov EBX,NUM
  • Call LISTADD
  • Mov EAX,SUM

Lokasi Subroutine LISTADD LISTADD Mov EAX,0 L1
Add EAX,EBX Add EBX,4 Loop L1 Ret
21
Passing Parameters via Stack Frame
Lokasi Program Utama Push NUM Push N
Call LISTADD Mov EAX,ESP Mov SUM,EAX
Add ESP,8
Lokasi Subroutine LISTADD LISTADDPush EAX
Push EBX Push ECX Mov ECX,ESP16
Mov EBX,ESP20 Mov EAX,0 L1
Add EAX,EBX Add EBX,4 Loop L1
Mov ESP16,EAX Pop ECX Pop EBX
Pop EAX Ret
ESPESP4ESP8ESP12ESP16ESP20
ESPESP4
22
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

23
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

24
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
Write a Comment
User Comments (0)
About PowerShow.com