-3

I have recently started to study the concept of Assembly language and have been using the DosBox with 8086 processor to learn to code respectively in that. I recently came up with the ideas of procedures in Assembly Language and how to add different procedures into the same program, but after that I came upon a question in the book which has some kind of analogy with the high level language, and got me a little confused on how would I implement these procedures. The exact question is as follows:

To illustrate the concept of passing parameters by value and passing parameters by address, write appropriate procedures in assembly language for addition of two 16 bit integers. Assume that no overflow will occur after addition. Write also the corresponding main code.

I would appreciate if someone could help me move forward in how to write this particular code for the procedures.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    Presumably you are expected to write the equivalent of `void add(int x , int y, int& result);`. Which part is causing you problem? Are you given a particular calling convention? – Jester Sep 10 '21 at 13:12
  • Yes, the main difference that I need to show is in the passing part, i.e. but here how would I show the difference. Like, one thing that I think would be possible for this is that in case of passing by value I would write the added answer in a new memory location and use that as the returned answer and before exiting the procedure would restore the initial values of the used memory locations from the stack, but in case of by parameters i would just update the result memory location and not restore its value. Would this be the correct answer interpretation of the question.. – Singularity Sep 10 '21 at 13:22
  • I assume you mean "came across", if you're reading a book with examples. Unless you really did "come up with" the idea independently, and then go buy a book about it... – Peter Cordes Sep 10 '21 at 13:23
  • 1
    Since the procedure has 3 arguments, you can demonstrate both kinds with one code. In my example, `x` and `y` are passed by value but the `result` is passed by address. – Jester Sep 10 '21 at 13:24
  • Yeah, I came across this question in a book but the answer for that question is not particularly available, and hence I asked here. I just wanted to find some new interpretation of the answer, or get some inputs on my answer, i.e. is the method correct or needs some changes. – Singularity Sep 10 '21 at 13:26
  • 1
    I'd assume the book means "input parameters" - e.g. the equivalents of `int add(int a, int b) { return a + b; }` and `int add(int * a, int * b) { return *a + *b; }` in whichever assembly language. – Brendan Sep 10 '21 at 16:17

1 Answers1

3

Because you are new to assembly, I will say this: writing in assembly, you have many options. You need to make some choices before you code. And you have DEBUG in DOSBOX. Use it to see how your code is working.

It is quite common to return an integer result in AX.

If you choose a calling convention that passes arguments in registers, then you might add 2+3 by value like this (16-bit 8086 code) :

 mov ax,0002h  ; ax will contain result on return
 mov bx,0003h
 call add_int
 ; expect AX=0005h here
...
add_int:
 add ax,bx
 ret

Or you might choose to PUSH the two values on the stack before CALL and get the result in AX, in which case you need to remember to recover stack space after the CALL. For 16-bit 8086 that means add sp,0004h. (Corrected after a comment by Peter Cordes).

You have the same choice for passing arguments by address. You could use LEA to get the addresses of arguments, but you must choose whether to pass the addresses for CALL in registers (perhaps SI and DI?), or whether to PUSH them on the stack before CALL.

My last comment is to play around. Look at how simple C code runs using DEBUG and understand how the instructions operate. For example, https://en.wikipedia.org/wiki/X86_instruction_listings#Original_8086/8088_instructions lists x86 instructions. I expect DOSBOX supports later instructions, but I have not checked this.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • 1
    `add sp, 4` would be the right thing to pop two 16-bit args you pushed. `add sp, 8` would be four pushes. Otherwise yeah, also agreed that looking at compiler output can be helpful, especially for modern x86 (32 or 64-bit code) since it's more compiler-friendly. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Sep 12 '21 at 21:06