Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 10-28-2001, 11:08 PM   #1
Member (3 bit)
 
Join Date: Sep 2001
Posts: 5
Question assembler language

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.
ignorant1 is offline   Reply With Quote
Old 10-29-2001, 03:06 AM   #2
Member (10 bit)
 
Join Date: Jan 2001
Location: Birmingham, UK.
Posts: 553
Send a message via Yahoo to dj4uk
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.
dj4uk is offline   Reply With Quote
Old 10-29-2001, 10:49 AM   #3
Member (12 bit)
 
Paul Victorey's Avatar
 
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
I have to go to class now, but I can explain this better when I return
__________________
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.
Paul Victorey is offline   Reply With Quote
Old 10-29-2001, 03:32 PM   #4
Member (12 bit)
 
Paul Victorey's Avatar
 
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.
Let's look at how this works. First, we push 14 and 5 onto the stack, so the stack looks like this as it is about to enter addStuff:

Code:
; Param 2 (14)   ;   (ESP + 4)
; Param 1 (5)    ;   (ESP)
Now, when it calls the subroutine using call, it pushes the return address onto the stack, so the stack looks like:

Code:
; Param 2 (14)   ;   (ESP + 8)
; Param 1 (5)    ;   (ESP + 4)
; Return Address ;   (ESP)
Note that here, every push or pop changes the relative location of each variable. This isn't good; if we used pushes and pops in our subroutine, we'd always have to be paying very careful attention to where each variable is. So we use a trick -- we set the base pointer (EBP) to the value of the stack pointer. ESP may change as things push and pop, but EBP will stay the same as long as we don't explicitly change it, so our variables will always be in the same relative positions.

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
The very first line does just what we had decided we want to do -- save the value of ebp. At this point, the stack looks like this:

Code:
; Param 2 (14)   ;   (ESP + 12)
; Param 1 (5)    ;   (ESP + 8)
; Return Address ;   (ESP + 4)
; Saved EBP      ;   (ESP)
The second line, mov ebp, esp makes ebp point at the same place as esp. Now, we can ALWAYS know where our variables are in relation to EBP, and we can push and pop as much as we want without altering EBP. We still cannot pop more than we push; that's always a rule. Every push should have a pop (or equivalent, as we'll see a little later).

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)
OK, now let's look at the actual "meat" of the procedure:
Code:
  mov eax,dword ptr[ebp +8]
  add eax,dword ptr[ebp +12]
In the first line, we load parameter 1 (5) into eax. In the next step, we add parameter 2 (14) to eax, so eax (which is where we return our value) has 19, which is the value we want to return. So now, it's time to cleanup and exit.

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)
which is exactly what we want. Let's make sure we've done everything we needed:

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.
Paul Victorey is offline   Reply With Quote
Old 10-30-2001, 02:03 AM   #5
Member (10 bit)
 
Join Date: Jan 2001
Location: Birmingham, UK.
Posts: 553
Send a message via Yahoo to dj4uk
Now that is a better expanation!
dj4uk is offline   Reply With Quote
Old 11-12-2001, 12:03 PM   #6
Tam
Member (2 bit)
 
Join Date: Nov 2001
Location: Washington
Posts: 3
Send a message via Yahoo to Tam
Question

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.
Tam is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:37 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2