iki10230 pengantar organisasi komputer kuliah no. 08: multi-module programs

of 33 /33
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber : 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization, ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB 7 April 2004 L. Yohanes Stefanus ([email protected]) Bobby Nazief ([email protected]) bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/

Author: brina

Post on 04-Feb-2016

37 views

Category:

Documents


0 download

Embed Size (px)

DESCRIPTION

IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs. Sumber : 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization , ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB. 7 April 2004 - PowerPoint PPT Presentation

TRANSCRIPT

AT90S8515Sumber:
2. Hamacher. Computer Organization, ed-5
3. Materi kuliah CS61C/2000 & CS152/1997, UCB
7 April 2004
sum += i;
loop_start:
loop_start:
push edx ; save i on stack
push dword input ; push address on input on stack
call get_int
...
; address of word to store input into (at [ebp + 8])
; Notes:
get_int:
pop ebp
[ESP]
[EBP]
int add_scale(int x, int y, int sc)
{
...
cvec[i] = add_scale(avec[i], bvec[i], scale);
...
}
*
_add_scale:
jle L7 ; i < 2
push eax
push eax
call _add_scale
...
*
{
...
cvec[i] = add_scale(avec[i], bvec[i], scale);
...
}
*
{
z[i] = add_scale(x[i], y[i], scale);
}
...
...
}
*
add ecx,4*3
call _add_scale
inc edx
cmp edx,3
jb L1
inc edx
cmp edx,3
jb L1
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,[esp+4] ; esp as pointer
or, Intel has provided EBP register for this purpose
mov ebx,[ebp+4] ; ebp as pointer
To store local variables
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
*
subroutine_label:
; parameters passed by the calling program
sub esp,LOCAL_BYTES ; = # bytes needed by locals
; subprogram code
ret
ESP
LocVar-m
{
sum += i;
*sump = sum;
mov ebx, 1 ; i = 1
for_loop:
jnle end_for
inc ebx ; i++
jmp short for_loop
mov eax, [ebp-4] ; eax = sum
mov [ebx], eax ; *sump = sum
mov esp, ebp
subprogram_label:
; subprogram code
; parameters passed by the calling program
sub esp,LOCAL_BYTES ; = # bytes needed by locals
; subprogram code
ret
int add_scale(int x, int y, int scale)
{
...
cvec[i] = add_scale(avec[i], bvec[i], scale);
...
}
*
MOV EAX,VAR1
Bandingkan dengan:
MOV EAX,[EBX+ESI] ; EAX [[EBX] + [ESI]]
*
_main:
jle L7 ; i < 2
push eax
push eax
call _add_scale
...
void v_add_scale(int x[ ], int y[ ], int z[ ], int scale)
{
z[i] = add_scale(x[i], y[i], scale);
}
...
_main:
push eax
mov edx,[eax+edx] ; BVEC[i]
push edx
mov edx,[eax+edx] ; AVEC[i]
push edx
call _add_scale
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”
*
push eax
push eax
call _add_scale
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?
0xBFFD
00h
0xBFFE
34h
0xBFFF
12h
0xC000
00h
0xC001
43h
0xC002
12h
0xC003
00h
0xC004
00h
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
inc esi ; esi = 1001
; [esi]b aru = 33
add esi,2 ; esi = 1003
; [esi]baru = 33
; [var_a] = 11, 33333311