OK, so, how is the syntax? Let's suppose we'd like to declare a procedure called printoutáthat take a word as a parameter. Let's think that it is the print out discussed in chapter 8. We'd like to make it into a procedure now. So, the only thing we'd pass here is the word pointer that will go to DX.
|proc printout msgptr: word mov dx, [msgptr] mov ah, 9 int 21h retendp||printout proc msgptr: word mov dx, msgptr mov ah, 9 int 21h retprintout endp|
Hmm .. it's pretty similar, is not it? Note that MASM does not need the square bracket in referencing the parameters whereas TASM does. Note also that you have to mentionáthe retáinstruction to make sure that your subroutine properly returns to its caller. If you omit it, the processor will continue executing whatever instructions beneath it. So be careful! Beginning assembly programmer tends to forget this because of the features in high level languages.
Then how can we invoke it?
|call printout, offset message||invoke printout, offset message|
No big deal. In TASM we use the instruction calládirectly whereas in MASM we ought to use their proprietary instruction invoke. The invokeáwill be translated down to calláafter it is boiled down to executable code, however.
Now for the truth: The Intel x86 processor does provide a calláinstruction. However, the original calládoes NOTátake any parameter at all. So, how does the assembler able to accept parameters? Well, that parameterized calláor invokeáinstruction will be translated down to the original call. Then, the parameters are passed down through stack using pusháand pop. The transformation is automatic (and also tends to be pretty boring too). So, you do not have to do the same thing over and over again. If you're curious, the actual code would be like this: (Warning: a bit tech notes below)push offset message call printout add sp, 2
Note the last instruction. The SP register is added by 2. Why? We must restore the stack register too. You can do a popáinstead, but the popáneeds a victim register, which may not be available. That's why the assembler choose the add sp, 2áinstead. If you're confused about this, just forget it. This has been taken care of by assembler for you. :-)
áRegisters | áInterrupts | áCOM Structure | áMaking Labels | áVariables Declaration | áVariable Limits and Negative Values | áMoving Around Values | áCaveats in MOVs | áImpacts on Registers | áQuestion Marks On Variables |