Subroutine Syntax

  1. Points) Syntax. Organise the correct order of words in Special question. Write your answers (letters A, B, C or D) in the boxes.
  2. Points) Syntax. Organise the correct order of words in Special question. Write your answers (letters A, B, C, D or E) in the boxes.

OK, so, how is the syntax? Let's suppose we'd like to declare a procedure called printoutthat 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 mentionthe retinstruction 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 calldirectly whereas in MASM we ought to use their proprietary instruction invoke. The invokewill be translated down to callafter it is boiled down to executable code, however.

Now for the truth: The Intel x86 processor does provide a callinstruction. However, the original calldoes NOTtake any parameter at all. So, how does the assembler able to accept parameters? Well, that parameterized callor invokeinstruction will be translated down to the original call. Then, the parameters are passed down through stack using pushand 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 popinstead, but the popneeds a victim register, which may not be available. That's why the assembler choose the add sp, 2instead. 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 |

© -