|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (3 bit)
Join Date: Sep 2001
Posts: 5
|
i'm in an intro to computers class, and needless to say i don't know much about programming. i've got to write an assembler language program using subroutines(only like 3 or 4). truthfully, i don't even know what that means. the book does a horrible job explaining subroutines, and my teacher barely covered it. so if anyone has any suggestions, i would be very appreciative.
|
|
|
|
|
|
#2 |
|
Member (10 bit)
|
A subroutine is a piece of code within the main code that can be "called".
Basically this piece of code can be something that maybe used many times e.g. a conversion from decimal to hex. Also subrountines are used to modularise code. Pseudocode example: sub one () various code to execute function ................................. end sub sub two () various code to execute function ................................. end sub if (variable==true) then one else two end if I used to use assembly with but for the life of me I can't remember a thing (the mind blocks out painful things! )HTH I know I ain't the best at explaining things. I'm sure Paul Victorey will come up with a better explanation. |
|
|
|
|
|
#3 |
|
Member (12 bit)
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
|
This is correct -- subroutines are essentially like functions -- they take parameters, return values, and can be called from multiple points in the program.
I don't know if you're forced to use C-style calling (which is vital to use if you're going to use a mixed-language program). I use this for programs which are primarily written in C++ but with ASM routines for things like MMX/3dNow/ other instructions not supported in C++. The following is a simple subroutine, which should work in MASM, for adding 2 numbers together. It uses the C-style calling convention (parameters are pushed onto the stack, last to first, and are removed by the calling function, not the subroutine). Code:
addStuff PROC
push ebp
mov ebp, esp ; we make ebp point to the beginning of the stack
; this is useful because esp may change from push/pop
mov eax,dword ptr[ebp +8] ; param 1
add eax,dword ptr[ebp +12]; param 2
pop ebp ; restore the state of the base pointer
ret ; return to calling function
addStuff ENDP
__________________
Paul M. Victorey ------------------ I am not responsible for any problems that may arise as a result of following my advice. This includes, but is not limited to, computer failure, loss of data, nuclear war, famine, boils, no clean laundry, your daughter running off with a biker gang, or armageddon. Take my advice at your own risk. |
|
|
|
|
|
#4 |
|
Member (12 bit)
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
|
Ok, here's more info on procedures (using C calling conventions):
C calling conventions say that: 1) A procedure may not alter the ESP, EBP, EBX registers (plus some others, like the various segment registers if you are dealing with real mode (segment:offset) addressing. If you are doing Win32 console programming (flat mode) these registers are useless, mostly. 2) Parameters to a function are passed on the stack, last parameter first. 3) The caller cleans up the stack. 4) The return is in EAX. Let's look at my example function, which simply adds two numbers, and how it can be called: Here's how it is called. Let's add 5 and 14: Code:
mov ax,14 ; put the second param on stack first. push ax mov ax,5 push ax call addStuff add esp,8 ;restore the stack ;Now, eax should hold the value 5 + 14 = 19. Code:
; Param 2 (14) ; (ESP + 4) ; Param 1 (5) ; (ESP) Code:
; Param 2 (14) ; (ESP + 8) ; Param 1 (5) ; (ESP + 4) ; Return Address ; (ESP) But wait -- there's a problem with this. EBP is one of the registers we aren't allowed to change in the subroutine. So, we must save the value, then change it to what we want it to be, then change it back before we leave the subroutine. Let's look again at the addStuff subroutine: Code:
addStuff PROC push ebp mov ebp, esp mov eax,dword ptr[ebp +8] add eax,dword ptr[ebp +12] pop ebp ret addStuff ENDP Code:
; Param 2 (14) ; (ESP + 12) ; Param 1 (5) ; (ESP + 8) ; Return Address ; (ESP + 4) ; Saved EBP ; (ESP) Now that we're thinking about the variable in terms of EBP, this is what they are: Code:
; Param 2 (14) ; (EBP + 12) ; Param 1 (5) ; (EBP + 8) ; Return Address ; (EBP + 4) ; Saved EBP ; (EBP) Code:
mov eax,dword ptr[ebp +8] add eax,dword ptr[ebp +12] We pushed one parameter on the stack, EBP. We must restore this, so we pop it. After the pop, but before the return, the stack looks like this: Code:
; Param 2 (14) ; (ESP + 8) ; Param 1 (5) ; (ESP + 4) ; Return Address ; (ESP) 1) Stack pointer is the same as when the subroutine began. ESP points to the return address. 2) Return value is in EAX. 3) EBP, EBX are the same as they were when we entered. So, it looks like we can go ahead and use the ret instruction to return back to the calling code. After we return, the stack now looks like this: ; Param 2 (14) ; (ESP + 4) ; Param 1 (5) ; (ESP) Now, we're in the calling function, just after the call command. We pushed 2 parameters (8 bytes total) onto the stack, so we must pop them, or, as I said, do an equivalent. it turns out that this instruction: pop eax is really the same as: mov eax,dword ptr[esp] add esp,4 As we don't care anymore about the parameters we pushed, we can remove them with the equivalent instruction to 2 pops: add esp, 8 and now the stack is exactly like it was before we did any of this, and eax now holds the desired result of the procedure call. Whew. This is long, and detailed, I know. I hope this helps -- if you still have questions, just ask, and I can try to clarify the parts you don't understand. Last edited by Paul Victorey; 10-29-2001 at 03:37 PM. |
|
|
|
|
|
#5 |
|
Member (10 bit)
|
Now that is a better expanation!
|
|
|
|
|
|
#6 |
|
Member (2 bit)
|
Hi I am also taking assembly language and programming and I don't fully understand it and it sounds like I have the same teacher as the other postee
There is a website that my instructor told us to go to that co-relates to our book http://www.ece.wpi.edu/~fjlooft/tasm. What I want to know is there a web site, (or sites) where you can get all the info and how to's for programming. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|