rincian vba

25
Rincian VBA - 7.2 Membuat Prosedur Global Stack (Page 2 of 5 ) (Page 2 of 5) Problem Masalah When you're writing an application, you often need to know the name of the current  procedure from within your code. Ketika Anda sedang menulis aplikasi, Anda sering  perlu untuk mengetahui nama prosedur saat ini dari dalam kode Anda. For example, if an error occurs, you'd like to be able to have a generic function handle the error and display the name of the procedure in which the error occurred (and all the procedures that have  been called on the way to get there). Sebagai contoh, jika kesalahan terjadi, Anda ingin dapat memiliki fungsi umum menangani error dan menampilkan nama prosedur di mana kesalahan terjadi (dan semua prosedur yang telah disebut dalam perjalanan ke sana ). VBA doesn't include a way to retrieve this information. VBA tidak termasuk cara untuk mengambil informasi ini. How can you acco mplish this? Bagaimana Anda melakukannya? Solution Solusi By maintaining a list of active procedures, add ing the current name to the list on the way into the procedure and removing it on the way out, you can always keep track of the current procedure and the procedure calls that got you there. Dengan mempertahankan daftar prosedur aktif, menambahkan nama saat ini ke daftar di jalan ke prosedur dan menghapus itu di jalan keluar, Anda dapat selalu melacak prosedur saat ini dan panggilan  prosedur yang membuat Anda di sana. There are many other uses for this functionality (see the next solution, for example), b ut one simple use is to retrieve the name of the current procedure in a global error-handling procedure. Ada banyak kegunaan lain untuk fungsi ini (lihat solusi berikutnya, misalnya), tetapi satu menggunakan sederhana adalah dengan mengambil nama prosedur saat ini dalam kesalahan prosedur penanganan global. The kind of data structure you'll need for maintaining your list is called a  stack . Jenis struktur data yang Anda perlukan untuk memelihara daftar Anda disebut  stack. As you enter a new procedure, you "push" its name onto the top of the stack. Ketika Anda memasukkan prosedur baru, Anda "push" nama ke atas tumpukan. When you leave the  procedure, you "pop" the name off the stack. Ketika Anda meninggalkan prosedur, Anda "pop" nama dari stack. Figure 7-2 shows a graphical representation of a p rocedure stack in action. Gambar 7-2 menunjukkan gambaran grafis dari prosedur stack dalam tindakan. The arrows indicate the direction in which the stack grows and shrinks as you add and remove items. Tanda panah menunjukkan arah di mana tumpukan tumbuh dan menyusut ketika Anda menambahkan dan menghapus item.

Upload: colourfull-haliza

Post on 09-Apr-2018

255 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 1/25

Rincian VBA - 7.2 Membuat Prosedur

Global Stack 

(Page 2 of 5 ) (Page 2 of 5)

Problem Masalah 

When you're writing an application, you often need to know the name of the current procedure from within your code. Ketika Anda sedang menulis aplikasi, Anda sering

 perlu untuk mengetahui nama prosedur saat ini dari dalam kode Anda. For example, if an

error occurs, you'd like to be able to have a generic function handle the error and displaythe name of the procedure in which the error occurred (and all the procedures that have

 been called on the way to get there). Sebagai contoh, jika kesalahan terjadi, Anda ingin

dapat memiliki fungsi umum menangani error dan menampilkan nama prosedur di manakesalahan terjadi (dan semua prosedur yang telah disebut dalam perjalanan ke sana ).

VBA doesn't include a way to retrieve this information. VBA tidak termasuk cara untuk 

mengambil informasi ini. How can you accomplish this? Bagaimana Anda

melakukannya?

Solution Solusi 

By maintaining a list of active procedures, adding the current name to the list on the way

into the procedure and removing it on the way out, you can always keep track of the

current procedure and the procedure calls that got you there. Dengan mempertahankan

daftar prosedur aktif, menambahkan nama saat ini ke daftar di jalan ke prosedur danmenghapus itu di jalan keluar, Anda dapat selalu melacak prosedur saat ini dan panggilan

 prosedur yang membuat Anda di sana. There are many other uses for this functionality(see the next solution, for example), but one simple use is to retrieve the name of the

current procedure in a global error-handling procedure. Ada banyak kegunaan lain untuk 

fungsi ini (lihat solusi berikutnya, misalnya), tetapi satu menggunakan sederhana adalah

dengan mengambil nama prosedur saat ini dalam kesalahan prosedur penanganan global.

The kind of data structure you'll need for maintaining your list is called a stack . Jenis

struktur data yang Anda perlukan untuk memelihara daftar Anda disebut stack. As you

enter a new procedure, you "push" its name onto the top of the stack. Ketika Anda

memasukkan prosedur baru, Anda "push" nama ke atas tumpukan. When you leave the procedure, you "pop" the name off the stack. Ketika Anda meninggalkan prosedur, Anda

"pop" nama dari stack. Figure 7-2 shows a graphical representation of a procedure stack 

in action. Gambar 7-2 menunjukkan gambaran grafis dari prosedur stack dalam tindakan.The arrows indicate the direction in which the stack grows and shrinks as you add and

remove items. Tanda panah menunjukkan arah di mana tumpukan tumbuh dan menyusut

ketika Anda menambahkan dan menghapus item.

Page 2: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 2/25

Figure 7-2. The call stack and the sample routines to fill it  Gambar 7-2.  Panggilan stack dan sampel rutinitas untuk mengisinya 

To see the procedure stack in action, load 07-02.MDB . Untuk melihat prosedur tumpukan dalam tindakan, beban 07-02.MDB. Open the module basTestStack in design

mode. Buka basTestStack modul dalam modus desain. Open the Immediate window

(choose View Immediate Window). Buka jendela segera (pilih View Segera Window). Inthe Immediate window, type: Pada jendela segera, ketik:

? ? A( ) A () 

to execute the function named A. Figure 7-2 shows A and the procedures it calls. untuk menjalankan fungsi bernama A. Gambar 7-2 menunjukkan A dan prosedur itu panggilan.

At each step, the current procedure pushes its name onto the procedure stack and then

calls some other procedure. Pada setiap langkah, prosedur saat ini mendorong namanyake prosedur stack dan kemudian memanggil beberapa prosedur lainnya. Once the calling

 procedure regains control, it pops its name off of the stack. Setelah memanggil prosedur 

mendapatkan kembali kontrol, itu muncul nama off dari stack. In addition, each

 procedure prints the name of the current procedure (using the acbCurrentProc function,discussed later in this solution) to the Immediate window. Selain itu, setiap prosedur 

mencetak nama prosedur saat ini (menggunakan fungsi acbCurrentProc, dibahas

kemudian dalam larutan ini) ke jendela segera. Once all execution has finished, youshould see in the Immediate window output like that shown in Figure 7-3. Sekali

 pelaksanaan semua selesai, Anda akan melihat di jendela output segera seperti yang

ditunjukkan dalam Gambar 7-3.

Page 3: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 3/25

Figure 7-3. The output from running the sample procedure Gambar 7-3. Output darimenjalankan prosedur sampel  

Follow these steps to incorporate this functionality into your own applications: Ikutilangkah-langkah untuk menggabungkan fungsi ini ke dalam aplikasi Anda sendiri:

1. Import the module basStack into your application. Impor basStack modul ke

dalam aplikasi Anda. This includes the procedures that initialize and maintain the

 procedure stack. Hal ini mencakup prosedur yang menginisialisasi danmempertahankan prosedur tumpukan.

2. Insert a call to the acbInitStack subroutine into code that's executed when your 

application starts up. Masukkan panggilan ke subrutin acbInitStack ke dalam kode

yang dijalankan ketika aplikasi Anda dijalankan. Consider adding this procedurecall to the code in your main form's Load event procedure. Pertimbangkan untuk 

menambahkan ini pemanggilan prosedur untuk kode dalam prosedur acara bentuk 

utama Anda Load. You'll want to call acbInitStack any time you restart your  program during development, so you probably don't want to call it from the

Autoexec macro, which is executed only when you first load the database. Anda

akan ingin menelepon acbInitStack setiap kali Anda me-restart program andaselama pengembangan, sehingga Anda mungkin tidak ingin menyebutnya dari

makro Autoexec, yang dieksekusi hanya ketika Anda pertama kali membuka

database. To call acbInitStack , either place its name alone on a line of code, likethis: Untuk memanggil acbInitStack, baik tempat namanya sendiri di sebuah baris

kode, seperti ini:

acbInitStack acbInitStack 

or use the Call construct, as follows: atau menggunakan Panggil membangun,

sebagai berikut:

Page 4: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 4/25

Call acbInitStack Call acbInitStack 

3. For each procedure in your application, place a call to acbPushStack as the first

statement. Untuk setiap prosedur dalam aplikasi Anda, tempat panggilan untuk 

acbPushStack sebagai pernyataan pertama. This procedure will place the value it's passed on the top of the stack. Prosedur ini akan menempatkan nilai itu berlalu di

atas tumpukan. As the single argument for each call, pass the name of the current

 procedure. Sebagai argumen tunggal untuk setiap panggilan, melewati nama

 prosedur saat ini.

Our example places a pair of parentheses after function names and nothing after 

subroutine names, as a matter of style. contoh kami menempatkan sepasangkurung setelah nama fungsi dan tidak ada setelah nama subrutin, sebagai masalah

gaya. As the last line in each procedure add a call to acbPopStack , which will

remove the current name from the top of the stack. Sebagai baris terakhir di setiap

 prosedur menambahkan panggilan ke acbPopStack, yang akan menghapus namasaat ini dari atas tumpukan.

4. You can retrieve the name of the currently executing procedure at any time by

calling the acbCurrentProc function. Anda dapat mengambil nama melaksanakan prosedur saat ini setiap saat dengan memanggil fungsi acbCurrentProc. This

function looks at the top of the stack and returns the string it finds there. Fungsi

ini tampak di atas tumpukan dan mengembalikan string yang ditemukan di sana.You can use this as part of an error handler or, as in the next solution, to help

track procedure performance. Anda dapat menggunakan ini sebagai bagian dari

 pengendali kesalahan atau, seperti dalam solusi berikutnya, untuk membantumelacak performa prosedur.

Discussion Diskusi 

The module you imported from 07-02.MDB , basStack, includes code for maintaining the

 procedure stack and a module-local variable that is the stack itself. Modul Anda yangdiimpor dari 07-02.MDB, basStack, termasuk kode untuk mempertahankan prosedur 

tumpukan dan variabel lokal modul yang stack itu sendiri. There are just six entry points

(nonprivate procedures) in the module. Ada titik masuk hanya enam (prosedur 

nonprivate) pada modul. Table 7-1 lists those procedures. Tabel 7-1 daftar prosedur tersebut. Since all the code for the stack is encapsulated in that one module, you never 

really have to know how it all works. Karena semua kode untuk stack yang dikemas

dalam satu modul, Anda tidak pernah benar-benar harus tahu bagaimana semuanya bekerja. However, it's quite simple. Namun, cukup sederhana.

Table 7-1. Tabel 7-1. The six entry points into basStack  Entry point enam ke basStack  

Procedure name

 Nama prosedur  Purpose Tujuan  Parameters Parameter  

Page 5: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 5/25

acbInitStack 

acbInitStack  Initialize the stack. Menginisialisasi stack. 

acbPushStack 

acbPushStack  Add an item to the stack.

Tambahkan item ke stack. A string to push string

untuk mendorong 

acbPopStack 

acbPopStack  Remove an item from the stack. Hapus item dari stack. 

acbCurrentProc

acbCurrentProc Retrieve the name of the current procedure. Ambil nama

 prosedur saat ini. 

acbGetStack 

acbGetStack  

Retrieve a specific item from the

stack. Ambil item tertentu dari

stack. 

The item number to

retrieve Jumlah item untuk 

mengambil 

acbGetStackItemsacbGetStackItems  Retrieve the number of items on the stack. Ambil jumlah item

 pada stack. 

 basStack includes two module-level variables: mastrStack , the array of strings that is

the stack itself; and mintStackTop , an integer that holds the array slot into which the

next stack item will be placed. basStack termasuk dua tingkat variabel modul:mastrStack, array dari string yang adalah stack itu sendiri, dan mintStackTop,

sebuah integer yang memegang slot array ke yang item stack berikutnya akan

ditempatkan. When you begin your work with the stack, mintStackTo p must be 0,

so the first item will go in the slot numbered 0. Ketika Anda mulai pekerjaan Andadengan tumpukan, p mintStackTo harus 0, maka item pertama akan masuk dalam

slot nomor 0. The acbInitStack procedure does nothing other than initializemintStackTop : Prosedur acbInitStack tidak melakukan apapun selain

menginisialisasi mintStackTop: 

Public Sub acbInitStack( ) Public Sub acbInitStack () ' Resets the stack top to 0. 'Mereset tumpukan atas untuk 0. mintStackTop = 0 mintStackTop = 0 End Sub End Sub 

You can add an item to the stack at any time by calling acbPushStack . Anda dapatmenambahkan item ke stack di setiap saat dengan menelepon acbPushStack. Pass to thissubroutine the item you want pushed. Pass subrutin ini item yang Anda inginkan

didorong. To push the item, the code places the item in the array at the location stored inmintStackTo p and then increments the value of mintStackTop . Untuk 

mendorong item, kode tempat item dalam array di lokasi yang tersimpan dalam p

mintStackTo lalu menaikkan nilai mintStackTop. Its code is: kode adalah:

Page 6: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 6/25

Public Sub acbPushStack(strToPush As String) Public SubacbPushStack (strToPush As String) 

' Push a string onto the call stack. 'Push string panggilanke stack. ' If the stack is full, display an error. 'Jika stack yangpenuh, menampilkan error. ' Otherwise, add the new item to the call stack. 'Jikatidak, tambahkan item baru ke panggilan tersebut stack. 

' Handle the error case first. "Menangani kasus kesalahanpertama. If mintStackTop > acbcMaxStack Then Jika> mintStackTopacbcMaxStack Kemudian MsgBox acbcMsgStackOverflow MsgBox acbcMsgStackOverflow Else Lain ' Store away the string. 'Simpan pergi string. 

mastrStack(mintStackTop) = strToPush mastrStack(mintStackTop) = strToPush 

' Set mintStackTop to point to the NEXT 'MintStackTop Setuntuk menunjuk ke NEXT ' item to be filled. 'Item yang akan diisi. mintStackTop = mintStackTop + 1 mintStackTop = mintStackTop+ 1 End If End If End Sub End Sub 

The only problem that might occur is that the stack might be full. Satu-satunya masalahyang mungkin terjadi adalah bahwa mungkin stack penuh. The constantacbcMaxStack is originally set to 20, which should be enough levels. The

acbcMaxStack konstan semula dijadwalkan untuk 20, yang harus tingkat cukup.

(Remember that mintStackTo p goes up one only when a procedure calls another 

 procedure. If you have 20 levels of procedure calling, you might consider rethinking your application, instead of worrying about procedure stacks!) If the stack is full,

acbPushStack will pop up an alert and will not add the item to the stack. (Ingat bahwa p

mintStackTo naik satu-satunya ketika prosedur panggilan prosedur lain,. Jika Anda

memiliki 20 tingkat prosedur untuk menelepon Anda mungkin mempertimbangkan

 pemikiran ulang aplikasi Anda, bukannya mencemaskan tumpukan prosedur!) Jika stack 

yang penuh, acbPushStack akan pop up peringatan dan tidak akan menambahkan item kestack.

When leaving a procedure, you'll want to remove an item from the stack. Ketika

meninggalkan prosedur, Anda akan ingin menghapus item dari stack. To do so, call theacbPopStack procedure: Untuk melakukannya, hubungi prosedur acbPopStack: 

Public Sub acbPopStack( ) Public Sub acbPopStack () 

Page 7: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 7/25

' Pop a string from the call stack. 'Pop string daripanggilan stack. ' If the stack is empty, display an error. 'Jika stackkosong, menampilkan kesalahan. ' Otherwise, set the current item to be the 'Jika tidak,mengatur item saat ini menjadi ' next one to be filled in. If you're logging, 'Satu disebelah diisi masuk Jika Anda login, ' send the information out to the log file. 'Mengiriminformasi keluar ke file log. 

' Handle the error case first. "Menangani kasus kesalahanpertama. If mintStackTop = 0 Then Jika mintStackTop = 0 Then MsgBox acbcMsgStackUnderflow MsgBox acbcMsgStackUnderflow Else Lain ' Because you're removing an item, not adding one, 'Karena

Anda menghapus item, tidak menambahkan satu, ' set the stack top back to the previous row. 'Mengaturtumpukan atas kembali ke baris sebelumnya. Next time Lainkali ' you add an item, it'll go right here. 'Anda menambahkansebuah barang, ia akan pergi di sini. mintStackTop = mintStackTop - 1 mintStackTop = mintStackTop- 1 End If End If End Sub End Sub 

Just as in acbPushStack , this code first checks to make sure that the stack integrity hasn't

 been violated; you can't remove an item from the stack if there's nothing to remove! Sama

seperti di acbPushStack, kode cek pertama untuk memastikan bahwa integritas stack tidak dilanggar, Anda tidak dapat menghapus item dari stack jika ada apa-apa untuk 

menghapus! If you try, acbPopStack will pop up an alert and exit. Jika Anda mencoba,acbPopStack akan muncul peringatan dan keluar. If the stack is intact, the procedure willdecrement the value of mintStackTop . Jika stack yang utuh, prosedur akan

 penurunan nilai mintStackTop. Decrementing that value sets up the next call to

acbPushStack so that it will place the new value where the old one used to be.

Decrementing nilai yang menentukan panggilan berikutnya ke acbPushStack sehingga

akan menempatkan nilai baru mana yang lama dulu.

To retrieve the value at the top of the stack without pushing or popping anything, call theacbCurrentProc function: Untuk mengambil nilai pada puncak stack tanpa mendorong

atau komentar apapun, memanggil fungsi acbCurrentProc: 

Public Function acbCurrentProc( ) As String Public FunctionacbCurrentProc () As String ' Since mintStackTop always points to the next item to

'Karena mintStackTop selalu menunjuk ke item berikutnya

Page 8: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 8/25

untuk ' be filled in, retrieve the item from mintStackTop - 1.

'Diisi, mengambil item dari mintStackTop - 1. If mintStackTop > 0 Then Jika mintStackTop> 0 Then acbCurrentProc = mastrStack(mintStackTop - 1) acbCurrentProc= mastrStack (mintStackTop - 1) Else Lain acbCurrentProc = "" acbCurrentProc = "" End If End If End Function End Function 

This function retrieves the value most recently placed on the stack (at the location one

less than mintStackTop , because mintStackTo p always points to the next

location to be filled). Fungsi ini mengambil nilai yang terakhir ditempatkan pada stack (di

lokasi yang kurang dari mintStackTop, karena p mintStackTo selalu menunjuk 

ke lokasi berikutnya yang akan diisi). You can't look at mastrStac k yourself,

 because it's local to basStack--and that's the way it ought to be. Anda tidak dapat melihat

k mastrStac diri sendiri, karena itu lokal untuk basStack - dan itulah cara seharusnya. Since the details of how the stack works are kept private, you can replace basStack, using a different architecture for the stack data structure, and the rest of your 

code won't have to change at all. Karena rincian tentang bagaimana tumpukan karya

disimpan pribadi, Anda dapat mengganti basStack, menggunakan arsitektur yang berbedauntuk struktur data stack, dan seluruh kode Anda tidak akan harus mengubah sama sekali.

To retrieve more information about what's in the stack, you can call acbGetStackItems ,

to find out how many items there are in the stack, and acbGetStack , which retrieves a

specific item from the stack. Untuk mengambil informasi lebih lanjut tentang apa yangada di tumpukan, Anda dapat menghubungi acbGetStackItems, untuk mengetahui berapa

 banyak item ada dalam tumpukan, dan acbGetStack, yang mengambil item tertentu daristack. For example, write code like this to dump out the entire stack (see subroutine  D ,

which does just this, in the basTestStack module): Misalnya, menulis kode seperti inimembuang keluar seluruh tumpukan (lihat A subroutine, yang tidak hanya ini, dalam

modul basTestStack):

Debug.Print "Stack items currently:" Debug.Print "Stack itemyang saat ini:" For intI = 0 To acbGetStackItems( ) - 1 Untuk inti = 0 UntukacbGetStackItems () - 1 Debug.Print , acbGetStack(intI) Debug.Print, acbGetStack

(INTI) Next intI Berikutnya inti 

The acbGetStackItems function is simple: it returns the value of mintStackTop ,

 because that value always contains the number of items in the stack: FungsiacbGetStackItems sederhana: ia mengembalikan nilai mintStackTop, karena nilai

yang selalu berisi jumlah item dalam tumpukan:

Page 9: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 9/25

Public Function acbGetStackItems( ) As Integer PublicFunction acbGetStackItems () As Integer ' Retrieve the number of items in the stack. "Ambil jumlahitem dalam tumpukan. acbGetStackItems = mintStackTop acbGetStackItems =mintStackTop End Function End Function 

The acbGetStack function is a little more complex. Fungsi acbGetStack sedikit lebih

kompleks. It accepts an item number (requesting item 0 returns the item at the top of thestack) and calculates the Ia menerima jumlah item (meminta item 0 mengembalikan item

tersebut di bagian atas tumpukan) dan menghitung

 posi tion of the item to retrieve. tion posisi item untuk mengambil. Its source code is:

kode sumber adalah:

Public Function acbGetStack(mintItem As Integer) As Strin g

Public Function acbGetStack (mintItem As Integer) Sebagai

Strin g ' Retrieve the item that's mintItems from the top of the

'Ambil item yang mintItems dari atas ' stack. 'Stack. That is, Artinya, ' ? '? acbGetStack(0) acbGetStack (0) ' would return the same value as acbCurrentProc. 'Akanmengembalikan nilai yang sama acbCurrentProc. ' ? '? acbGetStack(3) would return the third value from thetop. acbGetStack (3) akan mengembalikan nilai ketiga dariatas. If mintStackTop >= mintItem Then Jika mintItem =>mintStackTop Kemudian acbGetStack = mastrStack(mintStackTop - mintItem - 1)

acbGetStack = mastrStack (mintStackTop - mintItem - 1) Else Lain acbGetStack = "" acbGetStack = "" End If End If End Function End Function 

For the procedure stack to work, you have to place calls to acbPushStack andacbPopStack on entry and exit from every procedure call. Untuk prosedur stack untuk 

 bekerja, Anda harus menempatkan panggilan ke acbPushStack dan acbPopStack pada

entri dan keluar dari setiap pemanggilan prosedur. Good coding practice Bagus coding

 praktek supports the concept of only one exit point from each procedure, but even the best

 programmer sometimes breaks this rule. mendukung konsep hanya satu titik keluar dari

setiap prosedur, tapi bahkan programmer terbaik kadang-kadang melanggar peraturan ini.To use the call stack, however, you must catch every exit point with a call to

acbPopStack . Untuk menggunakan panggilan stack, bagaimanapun, Anda harus

menangkap setiap titik keluar dengan menelpon ke acbPopStack. Keep this in mind asyou retrofit old code to use this mechanism and when you devise new code to use it.

Page 10: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 10/25

Ingatlah ini ketika Anda retrofit kode lama untuk menggunakan mekanisme ini dan ketika

Anda menyusun kode baru untuk menggunakannya. You can always code for a single

exit point, and you will find code maintenance much easier if you do. Anda selalu dapatkode untuk titik keluar tunggal, dan Anda akan menemukan kode pemeliharaan jauh

lebih mudah jika Anda lakukan.

Page 11: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 11/25

PROSEDUR DAN FUNGSI (Bagian 4) 

11.5.9 - Passing Parameters on the Stack 11.5.9 - Parameter Melewati pada Stack

11.5.9 Passing Parameters on the Stack 11.5.9 Parameter Melewati pada Stack 

Most HLLs use the stack to pass parameters because this method is fairly efficient. KebanyakanHLLs menggunakan stack untuk melewati parameter karena metode ini cukup efisien. To passparameters on the stack, push them immediately before calling the subroutine. Untukmemberikan parameter di stack, mendorong mereka segera sebelum memanggil subrutin. Thesubroutine then reads this data from the stack memory and operates on it appropriately. subrutinkemudian membaca data dari memori stack dan beroperasi di atasnya tepat. Consider thefollowing Pascal procedure call: Pertimbangkan panggilan Pascal prosedur berikut: 

CallProc(i,j,k+4); CallProc (i, j, k +4);

Most Pascal compilers push their parameters onto the stack in the order that they appear in theparameter list. Kebanyakan kompiler Pascal mendorong parameter mereka ke dalam stackdalam urutan seperti yang terlihat dalam daftar parameter. Therefore, the 80x86 code typicallyemitted for this subroutine call (assuming you're passing the parameters by value) is Oleh karenaitu, kode 80x86 biasanya dipancarkan untuk panggilan subroutine (dengan asumsi Andamelewati parameter dengan nilai) adalah 

push i i pushpush j push jmov ax, k mov ax, kadd ax, 4 tambahkan kapak, 4push ax push axcall CallProc panggilan CallProc

Upon entry into CallProc , the 80x86's stack looks like that shown below (for a near or a far 

procedure). Setelah masuk ke CallProc , 80x86 tumpukan yang tampak seperti yang

ditunjukkan di bawah ini (untuk dekat atau prosedur yang jauh). 

Page 12: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 12/25

You could gain access to the parameters passed on the stack by removing the data from thestack (Assuming a near procedure call): Anda bisa mendapatkan akses ke parameter yangdikirimkan pada stack dengan menghapus data dari tumpukan (asumsi panggilan prosedur dekat): 

CallProc proc near CallProc proc dekat

pop RtnAdrs pop RtnAdrspop kParm pop kParmpop jParm pop jParmpop iParm pop iParmpush RtnAdrs push RtnAdrs. .. .. .ret membasahi

CallProc endp CallProc endp

There is, however, a better way. Ada, bagaimanapun, suatu cara yang lebih baik. The 80x86'sarchitecture allows you to use the bp (base pointer) register to access parameters passed on the

stack. 80x86's Arsitektur ini memungkinkan Anda untuk menggunakan bp (base pointer)mendaftar untuk mengakses parameter yang dikirimkan di stack. This is one of the reasons thedisp[bp], [bp][di], [bp][si], disp[bp][si], and disp[bp][di] addressing

modes use the stack segment rather than the data segment. Ini adalah salah satu alasandisp[bp], [bp][di], [bp][si], disp[bp][si], dan disp[bp][di] mode

pengalamatan menggunakan segmen stack daripada segmen data. The following code segmentgives the standard procedure entry and exit code: Segmen kode berikut memberikan entriprosedur standar dan kode keluar: 

StdProc proc near StdProc proc dekatpush bp push bpmov bp, sp mov bp, sp. .

. .. .pop bp pop bpret ParmSize ret ParmSize

StdProc endp StdProc endp

ParmSize is the number of bytes of parameters pushed onto the stack before calling theprocedure. ParmSize adalah jumlah byte parameter didorong ke stack sebelum meneleponprosedur. In the CallProc procedure there were six bytes of parameters pushed onto the stack

so ParmSize would be six. Dalam CallProc prosedur ada enam byte parameter didorong ke

stack sehingga ParmSize akan menjadi enam. 

Take a look at the stack immediately after the execution of mov bp, sp in StdProc . Lihatlah

tumpukan segera setelah pelaksanaan mov bp, sp di StdProc . Assuming you've pushedthree parameter words onto the stack, it should look something like shown below: Dengan asumsiAnda sudah mendorong tiga kata parameter ke stack, seharusnya terlihat seperti ditunjukkan dibawah ini: 

Page 13: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 13/25

Now the parameters can be fetched by indexing off the bp register: Sekarang parameter dapat

diambil oleh pengindeksan dari bp mendaftar: 

mov ax, 8[bp] ;Accesses the first parametermov ax, 8 [pb]; Akses parameter pertama

mov ax, 6[bp] ;Accesses the second parametermov ax, 6 [pb]; Akses parameter kedua

mov ax, 4[bp] ;Accesses the third parametermov ax, 4 [pb]; Akses parameter ketiga

When returning to the calling code, the procedure must remove these parameters from the stack.Ketika kembali ke kode panggilan, prosedur harus menghapus parameter dari stack. Toaccomplish this, pop the old bp value off the stack and execute a ret 6 instruction. Untuk

mencapai hal ini, pop yang lama bp nilai dari stack dan melaksanakan ret 6 instruksi. This

pops the return address off the stack and adds six to the stack pointer, effectively removing theparameters from the stack. Ini muncul alamat kembali dari tumpukan dan menambah enamsampai stack pointer, efektif menghilangkan parameter dari stack. 

The displacements given above are for near procedures only. When calling a far procedure,Perpindahan diberikan di atas adalah untuk prosedur dekat saja. Ketika memanggil sebuahprosedur yang jauh, 

• 0[BP] will point at the old BP value, 0 [BP] akan titik di nilai BP lama, • 2[BP] will point at the offset portion of the return address, 2 [BP] akan titik di bagian offset

dari alamat pengirim, • 4[BP] will point at the segment portion of the return address, and 4 [BP] akan titik di

bagian segmen dari alamat pengirim, dan • 6[BP] will point at the last parameter pushed onto the stack. 6 [BP] akan titik di parameter 

terakhir didorong ke stack. 

The stack contents when calling a far procedure are shown below: Tumpukan isi ketikamemanggil sebuah prosedur yang jauh adalah sebagai berikut: 

Page 14: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 14/25

This collection of parameters, return address, registers saved on the stack, and other items, is astack frame or activation record. Koleksi parameter, return address, register disimpan di stack,dan item lainnya, adalah stack frame atau merekam aktivasi. 

When saving other registers onto the stack, always make sure that you save and set up bp

before pushing the other registers. Saat menyimpan register lain ke stack, selalu pastikan bahwa

Anda menyimpan dan mengatur bp sebelum mendorong register lainnya. If you push the other registers before setting up bp , the offsets into the stack frame will change. Jika Anda mendorong

register lain sebelum mendirikan bp , offset ke dalam stack frame akan berubah. For example,

the following code disturbs the ordering presented above: Sebagai contoh, kode berikutmengganggu urutan yang disajikan di atas: 

FunnyProc proc near FunnyProc proc dekatpush ax push axpush bx push bxpush bp push bpmov bp, sp mov bp, sp. .. .

. .pop bp pop bppop bx pop bxpop ax pop axret membasahi

FunnyProc endp FunnyProc endp

Since this code pushes ax and bx before pushing bp and copying sp to bp , ax and bx appear in

the activation record before the return address (that would normally start at location [bp+2] ).

Karena kode ini mendorong ax dan bx sebelum mendorong bp dan menyalin sp untuk bp , ax

dan bx muncul dalam catatan aktivasi sebelum alamat pengirim (yang biasanya akan mulai di

lokasi [bp+2] ). As a result, the value of bx appears at location [bp+2] and the value of ax

appears at location [bp+4] . Sebagai hasilnya, nilai bx muncul di lokasi [bp+2] dan nilai ax

muncul di lokasi [bp+4] . This pushes the return address and other parameters farther up thestack as shown below: Hal ini mendorong alamat kembali dan parameter lain lebih jauhtumpukan seperti yang ditunjukkan di bawah ini: 

Although this is a near procedure, the parameters don't begin until offset eight in the activationrecord. Meskipun ini adalah prosedur dekat, parameter tidak dimulai sampai offset delapan dalamcatatan aktivasi. Had you pushed the ax and bx registers after setting up bp , the offset to the

parameters would have been four: Apakah Anda mendorong ax dan bx register setelah

menyiapkan bp , offset dengan parameter akan menjadi empat: 

Page 15: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 15/25

FunnyProc proc near FunnyProc proc dekatpush bp push bpmov bp, sp mov bp, sppush ax push axpush bx push bx. .. .. .pop bx pop bxpop ax pop axpop bp pop bp

ret membasahiFunnyProc endp FunnyProc endp

Therefore, the push bp and mov bp, sp instructions should be the first two instructions any

subroutine executes when it has parameters on the stack. Oleh karena itu, push bp dan mov

bp, sp instruksi harus dua yang pertama petunjuk subrutin setiap mengeksekusi ketika telah

parameter di stack. 

Accessing the parameters using expressions like [bp+6] can make your programs very hard to

read and maintain. Mengakses parameter menggunakan ekspresi seperti [bp+6] dapat

membuat program-program anda sangat sulit untuk membaca dan menjaga. If you would like touse meaningful names, there are several ways to do so. Jika Anda ingin menggunakan namayang bermakna, ada beberapa cara untuk melakukannya. One way to reference parameters by

name is to use equates. Salah satu cara untuk parameter referensi dengan nama adalah denganmenggunakan menyamakan. Consider the following Pascal procedure and its equivalent 80x86assembly language code: Perhatikan prosedur Pascal berikut dan 80x86 yang setara kodebahasa assembly: 

procedure xyz(var i:integer; j,k:integer); Prosedur xyz (vari: integer, j, k: integer);

begin mulaii := j+k; i: = j + k;

end; akhir;

Calling sequence: Memanggil urutan: 

xyz(a,3,4); xyz (a, 3,4);

Assembly language code: Bahasa assembly code: 

xyz_i equ 8[bp] ;Use equates so we canreference equ xyz_i 8 [bp]; Gunakan menyamakan sehingga kita dapatreferensixyz_j equ 6[bp] ; symbolic names in the body of

xyz_j equ 6 [bp]; nama simbolik dalam tubuh

Page 16: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 16/25

xyz_k equ 4[bp] ; the procedure. equ xyz_k] 4[pb; prosedur.xyz proc near xyz proc dekat

push bp push bpmov bp, sp mov bp, sppush es push espush ax push axpush bx push bxles bx, xyz_i ;Get address of I into ES:BX

les bx, xyz_i; Dapatkan alamat I ke ES: BXmov ax, xyz_j ;Get J parameter mov ax,

xyz_j; Dapatkan Parameter Jadd ax, xyz_k ;Add to K parameter

menambahkan kapak, xyz_k; Tambahkan ke parameter Kmov es:[bx], ax ;Store result into I parameter

mov es: [bx], ax; Simpan hasil ke dalam aku parameterpop bx pop bxpop ax pop axpop es pop espop bp pop bp

ret 8 ret 8xyz endp xyz endp

Calling sequence: Memanggil urutan: 

mov ax, seg a ;This parameter is passed bymov ax, seg sebuah; Parameter ini dilalui

push ax ; reference, so pass its pushax; referensi, sehingga lulus nya

mov ax, offset a ; address on the stack. movax, offset, alamat di stack.

push ax push axmov ax, 3 ;This is the second parameter

mov ax, 3; Ini adalah parameter keduapush ax push axmov ax, 4 ;This is the third parameter.

mov ax, 4; Ini adalah parameter ketiga.push ax push axcall xyz panggilan xyz

On an 80186 or later processor you could use the following code in place of the above: Pada80186 atau yang lebih baru prosesor yang Anda bisa menggunakan kode berikut di tempat diatas: 

push seg a ;Pass address of "a" on themendorong seg sebuah; alamat Pass dari "a" pada

push offset a ; stack. push offset; stack.push 3 ;Pass second parm by val. push

3; parm kedua Pass oleh val.push 4 ;Pass third parm by val. push

4; parm ketiga Pass oleh val.call xyz panggilan xyz

Page 17: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 17/25

Upon entry into the xyz procedure, before the execution of the les instruction, the stack looks

like shown below: Setelah masuk ke dalam xyz prosedur, sebelum pelaksanaan les instruksi,

stack tampak seperti ditunjukkan di bawah ini: 

Since you're passing I by reference, you must push its address onto the stack. Karena Andamelewati I oleh referensi, Anda harus mendorong alamatnya ke dalam stack. This code passesreference parameters using 32 bit segmented addresses. Kode ini menyampaikan beberapaparameter referensi menggunakan alamat 32 bit tersegmentasi.

 Note that this code uses

ret 8.

Perhatikan bahwa kode ini menggunakan ret 8 . Although there are three parameters on the

stack, the reference parameter I consumes four bytes since it is a far address. Meskipun ada tiga

parameter di stack, parameter referensi I mengkonsumsi empat byte karena merupakan alamat

 jauh. Therefore there are eight bytes of parameters on the stack necessitating the ret 8

instruction. Oleh karena itu ada delapan byte parameter pada stack yang mensyaratkan ret 8

instruksi. 

Were you to pass I by reference using a near pointer rather than a far pointer, the code wouldlook like the following: Apakah Anda lulus saya dengan referensi menggunakan pointer dekatdaripada yang jauh pointer, kode akan terlihat seperti berikut: 

xyz_i equ 8[bp] ;Use equates so we can

reference equ xyz_i 8 [bp]; Gunakan menyamakan sehingga kita dapatreferensixyz_j equ 6[bp] ; symbolic names in the body of

xyz_j equ 6 [bp]; nama simbolik dalam tubuhxyz_k equ 4[bp] ; the procedure. equ xyz_k] 4

[pb; prosedur.xyz proc near xyz proc dekat

push bp push bpmov bp, sp mov bp, sppush ax push axpush bx push bxmov bx, xyz_i ;Get address of I into BX mov

bx, xyz_i; Dapatkan alamat I ke BX

mov ax, xyz_j ;Get J parameter mov ax,xyz_j; Dapatkan Parameter Jadd ax, xyz_k ;Add to K parameter

menambahkan kapak, xyz_k; Tambahkan ke parameter Kmov [bx], ax ;Store result into I parameter

mov [bx], ax; Simpan hasil ke dalam aku parameterpop bx pop bxpop ax pop axpop bp pop bpret 6 ret 6

Page 18: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 18/25

Page 19: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 19/25

; xyz version using Pass by Value-Returned for xyz_i ; Xyz versidengan menggunakan Pass oleh Nilai-Returned untuk xyz_i

xyz_i equ 8[bp] ;Use equates so we canreference equ xyz_i 8 [bp]; Gunakan menyamakan sehingga kita dapatreferensixyz_j equ 6[bp] ; symbolic names in the body of

xyz_j equ 6 [bp]; nama simbolik dalam tubuhxyz_k equ 4[bp] ; the procedure. equ xyz_k] 4

[pb; prosedur.

xyz proc near xyz proc dekatpush bp push bpmov bp, sp mov bp, sppush ax push axpush bx push bxpush cx ;Keep local copy here. push

cx; Simpan salinan lokal di sini.

mov bx, xyz_i ;Get address of I into BX mov

bx, xyz_i; Dapatkan alamat I ke BXmov cx, [bx] ;Get local copy of I parameter.

mov cx, [bx]; Dapatkan copy lokal dari I parameter.

mov ax, xyz_j ;Get J parameter mov ax,xyz_j; Dapatkan Parameter J

add ax, xyz_k ;Add to K parametermenambahkan kapak, xyz_k; Tambahkan ke parameter K

mov cx, ax ;Store result into local copymov cx, ax; Simpan hasil ke dalam copy lokal

mov bx, xyz_i ;Get ptr to I, again mov bx,xyz_i; Dapatkan ptr ke I, lagi

mov [bx], cx ;Store result away. mov [bx],cx; Simpan hasil pergi.

pop cx pop cxpop bx pop bxpop ax pop axpop bp pop bpret 6 ret 6

xyz endp xyz endp

There are a couple of unnecessary mov instructions in this code. Ada beberapa instruksi movyang tidak perlu dalam kode ini. They are present only to precisely implement pass by value-returned parameters. Mereka hadir hanya untuk secara tepat menerapkan melewati nilai-parameter kembali. It is easy to improve this code using pass by result parameters. Sangat

mudah untuk memperbaiki kode menggunakan melewati parameter hasil. The modified code isKode dimodifikasi 

; xyz version using Pass by Result for xyz_i ; Xyz versi denganmenggunakan Pass oleh Hasil untuk xyz_i

xyz_i equ 8[bp] ;Use equates so we canreference equ xyz_i 8 [bp]; Gunakan menyamakan sehingga kita dapatreferensi

Page 20: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 20/25

xyz_j equ 6[bp] ; symbolic names in the body ofxyz_j equ 6 [bp]; nama simbolik dalam tubuhxyz_k equ 4[bp] ; the procedure. equ xyz_k] 4

[pb; prosedur.

xyz proc near xyz proc dekatpush bp push bpmov bp, sp mov bp, sppush ax push axpush bx push bxpush cx ;Keep local copy here. push

cx; Simpan salinan lokal di sini.

mov ax, xyz_j ;Get J parameter mov ax,xyz_j; Dapatkan Parameter J

add ax, xyz_k ;Add to K parametermenambahkan kapak, xyz_k; Tambahkan ke parameter K

mov cx, ax ;Store result into local copymov cx, ax; Simpan hasil ke dalam copy lokal

mov bx, xyz_i ;Get ptr to I, again mov bx,xyz_i; Dapatkan ptr ke I, lagi

mov [bx], cx ;Store result away. mov [bx],cx; Simpan hasil pergi.

pop cx pop cxpop bx pop bxpop ax pop axpop bp pop bpret 6 ret 6

xyz endp xyz endp

As with passing value-returned and result parameters in registers, you can improve the

performance of this code using a modified form of pass by value. Seperti dengan melewati nilai-kembali dan parameter mengakibatkan register, Anda dapat meningkatkan kinerja kode inidengan menggunakan bentuk modifikasi dari lewat nilai. Consider the following implementation of xyz: Pertimbangkan pelaksanaan xyz berikut: 

; xyz version using modified pass by value-result for xyz_i ; Versixyz menggunakan pass dimodifikasi oleh nilai-hasil untuk xyz_i

xyz_i equ 8[bp] ;Use equates so we canreference equ xyz_i 8 [bp]; Gunakan menyamakan sehingga kita dapatreferensixyz_j equ 6[bp] ; symbolic names in the body of

xyz_j equ 6 [bp]; nama simbolik dalam tubuh

xyz_k equ 4[bp] ; the procedure. equ xyz_k] 4[pb; prosedur.

xyz proc near xyz proc dekatpush bp push bpmov bp, sp mov bp, sppush ax push ax

mov ax, xyz_j ;Get J parameter mov ax,xyz_j; Dapatkan Parameter J

Page 21: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 21/25

add ax, xyz_k ;Add to K parametermenambahkan kapak, xyz_k; Tambahkan ke parameter K

mov xyz_i, ax ;Store result into local copymov xyz_i, kapak; Simpan hasil ke dalam copy lokal

pop ax pop axpop bp pop bpret 4 ;Note that we do not pop I

parm. ret 4; Perhatikan bahwa kita tidak pop saya parm.xyz endp xyz endp

The calling sequence for this code is Urutan menyerukan untuk kode inipush a ;Pass a's value to xyz.

mendorong; Pass nilainya untuk xyz.push 3 ;Pass second parameter by val.

push 3; parameter kedua Pass oleh val.push 4 ;Pass third parameter by val.

push 4; parameter ketiga Pass oleh val.call xyz panggilan xyzpop a pop

Note that a pass by result version wouldn't be practical since you have to push something on thestack to make room for the local copy of I inside xyz. Perhatikan bahwa lulus dengan hasil versitidak akan praktis karena Anda harus mendorong sesuatu di stack untuk membuat ruang bagicopy lokal dari I dalam xyz. You may as well push the value of a on entry even though the xyzprocedure ignores it. Anda mungkin juga mendorong nilai pada entri meskipun prosedur xyzmengabaikannya. This procedure pops only four bytes off the stack on exit. Prosedur ini munculhanya empat byte dari stack pada keluar. This leaves the value of the I parameter on the stack sothat the calling code can store it away to the proper destination. Ini meninggalkan nilai parameter I pada stack sehingga dapat menyimpan kode pemanggilan itu pergi ke tujuan yang tepat. 

To pass a parameter by name on the stack, you simply push the address of the thunk. Untukmelewati sebuah parameter dengan nama di stack, Anda cukup menekan alamat thunk tersebut.

Consider the following pseudo-Pascal code: Pertimbangkan kode pseudo-Pascal sebagai berikut:

procedure swap(name Item1, Item2:integer); Prosedur swap (nama item1,item2: integer);var temp:integer; var temp: integer;begin mulai

temp := Item1; temp: = item1;Item1 := Item2; Item1: = item2;Item2 := Temp; Item2: Temp =;

end; akhir;

If swap is a near procedure, the 80x86 code for this procedure could look like the following (notethat this code has been slightly optimized and does not following the exact sequence givenabove): Jika swap adalah prosedur dekat, kode 80x86 untuk prosedur ini bisa terlihat seperti)berikut (perhatikan bahwa kode ini telah sedikit dioptimalkan dan tidak mengikuti persis urutanyang diberikan di atas: 

; swap- swaps two parameters passed by name on the stack. ;Swap-swap dua parameter yang dikirimkan dengan nama di stack.

Page 22: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 22/25

; Item1 is passed at address [bp+6], Item2 is passed ;Item1 dilewatkan di alamat [bp +6], item2 dilewatkan; at address [bp+4] ; Di alamat [bp +4]

wp textequ <word ptr> wp textequ <word ptr>swap_Item1 equ [bp+6] swap_Item1 equ [bp +6]swap_Item2 equ [bp+4] swap_Item2 equ [bp +4]

swap proc near swap proc dekatpush bp push bpmov bp, sp mov bp, sppush ax ;Preserve temp value.

push ax; nilai temp Preserve.push bx ;Preserve bx. push bx;

bx Preserve.call wp swap_Item1 ;Get adrs of Item1.

panggilan wp swap_Item1; Dapatkan ADR dari item1.mov ax, [bx] ;Save in temp (AX).

mov ax, [bx]; Simpan di temp (AX).call wp swap_Item2 ;Get adrs of Item2.

panggilan wp swap_Item2; Dapatkan ADR dari item2.xchg ax, [bx] ;Swap temp <-> Item2.

xchg ax, [bx]; temp Swap <-> item2.call wp swap_Item1 ;Get adrs of Item1.

panggilan wp swap_Item1; Dapatkan ADR dari item1.mov [bx], ax ;Save temp in Item1.

mov [bx], ax; Simpan temp di item1.pop bx ;Restore bx. pop bx;

Kembalikan bx.pop ax ;Restore ax. pop ax;

Kembalikan kapak.ret 4 ;Return and pop

Item1/2. ret 4; Kembali dan pop item1 / 2.swap endp swap endp

Some sample calls to swap follow: Beberapa contoh panggilan ke swap berikut: 

; swap(A[i], i) -- 8086 version. ; Swap (A [i], i) - 8086 versi.

lea ax, thunk1 lea kapak, thunk1push ax push axlea ax, thunk2 lea kapak, thunk2push ax push axcall swap panggilan swap

; swap(A[i],i) -- 80186 & later version. ; Swap (A [i], i) - 80186 &kemudian versi.

push offset thunk1 push offset thunk1push offset thunk2 push offset thunk2call swap panggilan swap

. .

. .

. .

Page 23: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 23/25

; Note: this code assumes A is an array of two byte integers. ;Catatan: kode ini mengasumsikan adalah sebuah array dua bilangan bulatbyte.

thunk1 proc near thunk1 proc dekatmov bx, i mov bx, ishl bx, 1 SHL bx, 1lea bx, A[bx] lea bx, A [bx]ret membasahi

thunk1 endp thunk1 endp

thunk2 proc near thunk2 proc dekatlea bx, i lea bx, iret membasahi

thunk2 endp thunk2 endp

The code above assumes that the thunks are near procs that reside in the same segment as theswap routine. Kode di atas mengasumsikan bahwa thunks adalah procs dekat yang berada disegmen yang sama dengan rutinitas swap. If the thunks are far procedures the caller must passfar addresses on the stack and the swap routine must manipulate far addresses. Jika thunks jauh

prosedur pemanggil harus lulus jauh alamat pada stack dan rutin swap harus memanipulasi jauhalamat. The following implementation of swap, thunk1, and thunk2 demonstrate this.Pelaksanaan berikut swap, thunk1, dan thunk2 menunjukkan ini. 

; swap- swaps two parameters passed by name on the stack. ;Swap-swap dua parameter yang dikirimkan dengan nama di stack.; Item1 is passed at address [bp+10], Item2 is passed ;

Item1 dilewatkan di alamat [bp +10], item2 dilewatkan; at address [bp+6] ; Di alamat [bp +6]

swap_Item1 equ [bp+10] swap_Item1 equ [bp +10]swap_Item2 equ [bp+6] swap_Item2 equ [bp +6]dp textequ <dword ptr> dp textequ <dword ptr>

swap proc far proc swap jauhpush bp push bpmov bp, sp mov bp, sppush ax ;Preserve temp value. push ax;

nilai temp Preserve.push bx ;Preserve bx. push bx; bx

Preserve.push es ;Preserve es. push es; es

Preserve.call dp swap_Item1 ;Get adrs of Item1. panggilan

dp swap_Item1; Dapatkan ADR dari item1.mov ax, es:[bx] ;Save in temp (AX). mov ax,

es: [bx]; Simpan di temp (AX).call dp swap_Item2 ;Get adrs of Item2. panggilandp swap_Item2; Dapatkan ADR dari item2.

xchg ax, es:[bx] ;Swap temp <-> Item2. xchg ax,es: [bx]; temp Swap <-> item2.

call dp swap_Item1 ;Get adrs of Item1. panggilandp swap_Item1; Dapatkan ADR dari item1.

mov es:[bx], ax ;Save temp in Item1. mov es:[bx], ax; Simpan temp di item1.

Page 24: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 24/25

pop es ;Restore es. pop es;Kembalikan es.

pop bx ;Restore bx. pop bx;Kembalikan bx.

pop ax ;Restore ax. pop ax;Kembalikan kapak.

ret 8 ;Return and pop Item1, Item2.ret 8; Kembali dan pop item1, item2.swap endp swap endp

Some sample calls to swap follow: Beberapa contoh panggilan ke swap berikut: 

; swap(A[i], i) -- 8086 version. ; Swap (A [i], i) - 8086 versi.

mov ax, seg thunk1 mov ax, seg thunk1push ax push axlea ax, thunk1 lea kapak, thunk1push ax push axmov ax, seg thunk2 mov ax, seg thunk2push ax push axlea ax, thunk2 lea kapak, thunk2push ax push axcall swap panggilan swap

; swap(A[i],i) -- 80186 & later version. ; Swap (A [i], i) - 80186 &kemudian versi.

push seg thunk1 push seg thunk1push offset thunk1 push offset thunk1push seg thunk2 push seg thunk2push offset thunk2 push offset thunk2call swap panggilan swap

. .

. .

. .

; Note: this code assumes A is an array of two byteintegers. ; Catatan: kode ini mengasumsikan adalah sebuah array duabilangan bulat byte.; Also note that we do not know which segment(s) contain

; Juga dicatat bahwa kami tidak tahu mana segmen (s) mengandung; A and I. ; A dan I.

thunk1 proc far thunk1 proc jauhmov bx, seg A ;Need to return seg A in ES.

mov bx, seg A; Perlu untuk kembali seg A di ES.push bx ;Save for later. push bx;

Simpan untuk nanti.mov bx, seg i ;Need segment of I in order

mov bx, i seg; Butuh segmen I dalam rangkamov es, bx ; to access it. mov es, bx,

untuk mengaksesnya.mov bx, es:i ;Get I's value. mov bx, es: i;

Dapatkan nilainya saya.shl bx, 1 SHL bx, 1

Page 25: Rincian VBA

8/8/2019 Rincian VBA

http://slidepdf.com/reader/full/rincian-vba 25/25

lea bx, A[bx] lea bx, A [bx]pop es ;Return segment of A[I] in es.

pop es; Kembali segmen A [I] di es.ret membasahi

thunk1 endp thunk1 endp

thunk2 proc near thunk2 proc dekatmov bx, seg i ;Need to return I's seg in es.

mov bx, seg i; Perlu untuk kembali I seg di es.mov es, bx mov es, bxlea bx, i lea bx, iret membasahi

thunk2 endp thunk2 endp

Passing parameters by lazy evaluation is left for the programming projects. Parameter Passingoleh evaluasi malas yang tersisa untuk proyek-proyek pemrograman