AT90S8515 - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

AT90S8515

Description:

LEA Instruction. Load Effective Address. LEA REG,MEMLOC ; REG MEMLOC. LEA EAX,[VAR1] ; EAX VAR1. MOV EAX,VAR1 ... lea edx,[4*eax] mov eax,[ebp 12] ; BVEC. mov ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 34
Provided by: bobbyn4
Category:
Tags: at90s8515 | lea

less

Transcript and Presenter's Notes

Title: AT90S8515


1
IKI10230Pengantar Organisasi KomputerKuliah no.
08 Multi-module Programs
Sumber1. Paul Carter, PC Assembly Language2.
Hamacher. Computer Organization, ed-53. Materi
kuliah CS61C/2000 CS152/1997, UCB
7 April 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 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
  • implementasi dgn loop
  • (salah satu alternatif)
  • mov eax, 0 eax is sum
  • mov ecx, 10 ecx is (10-i)
  • mov edx, 0 edx is i
  • loop_start

3
KOREKSI 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

4
  • SubPrograms/SubRoutines/Functions

5
Subroutine
  • Contoh dalam program bahasa C
  • int add_scale(int x, int y, int sc)
  • int result
  • result x sc y
  • return result
  • main()
  • int avec3, bvec3, cvec3, scale
  • ...
  • for (i 0 i lt 3 i)
  • cveci add_scale(aveci, bveci,
    scale)
  • ...

6
Use of CALL RET in Subroutine Invocation
  • _add_scale
  • ...
  • mov eax,ebp16
  • imul eax,ebp12
  • add eax,ebp8
  • ...
  • ret
  • _main
  • ...
  • L4
  • cmp ebp-44,2 cmp(i, 2)
  • jle L7 i lt 2
  • jmp L5
  • L7
  • mov eax,ebp-40 scale
  • push eax
  • ...
  • mov eax,eaxedx bveci

7
  • Parameter Passing
  • between Calling- Callee-subroutines

8
Passing Parameters
  • By Value
  • By Reference

9
Passing Parameters By Value
  • int add_scale(int x, int y, int scale)
  • int result
  • result x scale y
  • return result
  • main()
  • int avec3, bvec3, cvec3, scale
  • ...
  • for (i 0 i lt 3 i)
  • cveci add_scale(aveci, bveci,
    scale)
  • ...

10
Passing Parameters By Reference
  • void v_add_scale(int x , int y , int z ,
    int scale)
  • for (i 0 i lt 3 i)
  • zi add_scale(xi, yi, scale)
  • main()
  • int avec3, bvec3, cvec3, scale
  • ...
  • v_acc_scale(avec, bvec, cvec, scale)
  • / cvec change after the call! /
  • ...

11
Implementation of Passing Parameters
  • Via Register
  • Via Stack

12
Passing Values via Registers
  • _main
  • mov esi,Scale
  • mov edx,0
  • L1
  • mov ecx,AVEC
  • mov eax,ecx4edx
  • add ecx,43
  • mov ebx,ecx4edx
  • call _add_scale
  • add ecx,43
  • mov ecx4edx,edi
  • inc edx
  • cmp edx,3
  • jb L1
  • _add_scale
  • mov edi,esi
  • imul edi,ebx
  • add edi,eax

13
Passing References via Registers
  • _main
  • mov eax,AVEC
  • mov ebx,BVEC
  • mov ecx,CVEC
  • mov esi,Scale
  • call _v_add_scale
  • ...
  • _v_add_scale
  • mov edx,0
  • L1
  • mov edi,esi
  • imul edi,ebx4edx
  • add edi,eax4edx
  • mov ecx4edx,edi
  • inc edx
  • cmp edx,3
  • jb L1
  • ret

14
Use of Stack
  • To pass parameters
  • due to limited number of registers
  • most HLLs use it as standard mechanism, known
    also as calling conventions
  • assembly programs/modules need to use these
    conventions to be able to communicate with HLL
    programs
  • data are accessed from the stack directly instead
    of via POP instruction
  • mov eax,esp4 esp as pointer
  • or, Intel has provided EBP register for this
    purpose
  • mov ebx,ebp4 ebp as pointer
  • To store local variables
  • func()
  • int x, y
  • ...

15
  • Stacks as Storage
  • for
  • Local Variables

16
Stack as Storage for Local Variables
  • Local variables live only as long as its
    function/ subroutine, where these variables
    reside, live (i.e., from the time the
    function/subroutine is called, until it returns
    execution to the calling program)
  • Accordingly, memory allocation for local
    variables does not need to be for the duration of
    the whole program
  • Stack is the convenient location for this
    temporary storage
  • Local variables are allocated on the stack, by
    extending the stack-frame used whenever a
    subroutine-call takes place (where return-address
    is placed)

17
General Form of a Subroutine
  • subroutine_label
  • push ebp save original EBP value on stack
  • mov ebp,esp new EBP ESP, needed to access
  • parameters passed by the calling program
  • sub esp,LOCAL_BYTES bytes needed by
    locals
  • subprogram code
  • mov esp,ebp deallocate locals
  • pop ebp restore original EBP value
  • ret

18
Example calc_sum.c
  • void calc_sum( int n, int sump )
  • int i , sum 0
  • for ( i1 i lt n i )
  • sum i
  • sump sum

19
Example calc_sum.asm
  • cal_sum
  • push ebp
  • mov ebp, esp
  • sub esp, 4
  • mov dword ebp - 4, 0 sum 0
  • mov ebx, 1 i 1
  • for_loop
  • cmp ebx, ebp12 is i gt n?
  • jnle end_for
  • add ebp-4, ebx sum i
  • inc ebx i
  • jmp short for_loop
  • end_for
  • mov ebx, ebp8 ebx sump
  • mov eax, ebp-4 eax sum
  • mov ebx, eax sump sum
  • mov esp, ebp
  • pop ebp
  • ret

20
General Form of a Subroutine using Enter/Leave
  • subprogram_label
  • enter LOCAL_BYTES, 0 bytes needed by
    locals
  • subprogram code
  • leave
  • ret
  • bandingkan dengan
  • subroutine_label
  • push ebp save original EBP value on stack
  • mov ebp,esp new EBP ESP, needed to access
  • parameters passed by the calling program
  • sub esp,LOCAL_BYTES bytes needed by
    locals
  • subprogram code
  • mov esp,ebp deallocate locals
  • pop ebp restore original EBP value
  • ret

21
  • Passing Parameters
  • via
  • Stack Frames

22
Passing Values via Stack Frames (1/2)
  • int add_scale(int x, int y, int scale)
  • int result
  • result x scale y
  • return result
  • main()
  • int avec3, bvec3, cvec3, scale
  • ...
  • for (i 0 i lt vec_length i)
  • cveci add_scale(aveci, bveci,
    scale)
  • ...

23
LEA Instruction
  • Load Effective Address
  • LEA REG,MEMLOC REG ? MEMLOC
  • LEA EAX,VAR1 EAX ? VAR1
  • MOV EAX,VAR1
  • LEA EAX,EBXESI EAX ? EBX ESI
  • Bandingkan dengan
  • MOV EAX,VAR1 EAX ? VAR1
  • MOV EAX,EBXESI EAX ? EBX ESI

24
Passing Values via Stack Frames (2/2)
  • _main
  • L4 cmp ebp-44,2 cmp(i, 2)
  • jle L7 i lt 2
  • jmp L5
  • L7
  • mov eax,ebp-40 scale
  • push eax
  • ...
  • lea eax,4edx
  • lea edx,ebp-24 bvec
  • mov eax,eaxedx bveci
  • push eax
  • ...
  • lea eax,4edx
  • lea edx,ebp-12 avec
  • mov eax,eaxedx aveci
  • push eax
  • call _add_scale
  • ...

_add_scale push ebp mov ebp,esp sub
esp,24 mov eax,ebp16 imul
eax,ebp12 mov edx,ebp8 add
edx,eax mov ebp-4,edx mov edx,ebp-4 mov
eax,edx mov esp,ebp pop ebp ret
25
Passing References via Stack Frames (1/2)
  • void v_add_scale(int x , int y , int z ,
    int scale)
  • for (i 0 i lt vec_length i)
  • zi add_scale(xi, yi, scale)
  • main()
  • int avec3, bvec3, cvec3, scale
  • ...
  • v_acc_scale(avec, bvec, cvec, scale)
  • ...

26
Passing References via Stack Frames (2/2)
_v_add_scale ... L4 cmp ebp-4,2 jle L7 jmp
L5 L7 mov eax,ebp20 scale push eax mov
eax,ebp-4 lea edx,4eax mov eax,ebp12
BVEC mov edx,eaxedx BVECi push
edx ... mov eax,ebp8 AVEC mov
edx,eaxedx AVECi push edx call
_add_scale ... mov edx,ebp16 mov
edxecx,eax inc ebp-4 jmp L4 L5 ... ret
  • _main
  • mov eax,ebp-40
  • push eax
  • lea eax,ebp-36
  • push eax
  • lea eax,ebp-24
  • push eax
  • lea eax,ebp-12
  • push eax
  • call _v_add_scale
  • add esp,16

27
  • Multi-module Programs

28
Multi-module Programs
  • A multi-module program is one composed of more
    than one object file.
  • tugas0a.exe driver.o tugas0a.o asm_io.o C
    library
  • Combined by the linker
  • gcc o tugas0a.exe driver.o tugas0a.o asm_io.o C
    library
  • The linker must match up references made to each
    label in one module (i.e. object file) to its
    definition in another module
  • Calling-module declare the called-label extern
  • Called-module declare the called-label global

29
Example
  • AddScale.asm
  • global _add_scale
  • _add_scale
  • ...
  • mov eax,ebp16
  • imul eax,ebp12
  • add eax,ebp8
  • ...
  • ret
  • Main.asm
  • global _main
  • extern _add_scale
  • _main
  • ...
  • L4
  • ...
  • mov eax,eaxedx bveci
  • push eax

30
  • On Pop-Quiz 1

31
No. 3
  • Jika diketahui variabel X adalah sebuah integer
    (4 byte) dengan alamat 0xBFFE. Sistem komputernya
    menggunakan format Little-Endian untuk menyimpan
    data multi-byte dalam memori yang
    byte-addressable. Berapa nilai X?

32
No. 4
  • Var i 1001b (integer 4-bit) dan j 00010000b
    (integer 8-bit), keduanya menggunakan
    representasi komplemen-2. Hitung k i j!
    (nyatakan dalam biner)
  • i 1001 11111001 -7
  • j 00010000 16
  • k 00001001 9

33
No. 5
  • Perhatikan bagian program berikut
  • segment .data
  • var_a dd 44332211h
  • var_a db 11h,22h,33h,44h
  • segment .text
  • mov esi,var_a
  • mov al,esi esi 1000 , al 11
  • inc esi esi 1001
  • add esi,al esilama 22
    , al 11
  • esib aru 33
  • add esi,2 esi 1003
  • sub esi,al esilama 44
    , al 11
  • esibaru 33
  • var_a 11, 33333311
Write a Comment
User Comments (0)
About PowerShow.com