0

Now, I want to call a subroutine by value.

My source said you do so by

call subroutineName, (var1, var2)

subroutineName(var3, var4):
    ;code

I don't know how, it gives me a "invalid combination of opcode and operands"

I also tried formatting it differently (putting a colon in between, putting nothing in between and so on.

https://people.cs.clemson.edu/~mark/sparc/subroutines.txt here is the source and what he wrote:

     main
       a = 1           a: 1
       b = 2           b: 2
       call subr(a,b)  pass 1,2 via stack
       print a,b                            print 1,2

     subr(x,y)         copy 1,2 into x,y    ^
       x = x + 1       x: /1/ 2             |
       y = x + y       y: /2/ 4             |
       return          ---------------------'

I am really confused to how to call and return with value and result.

Thanks for your Help though!

Martin
  • 37
  • 1
  • 10
  • 2
    That's pseudo code, from a document titled _Subroutines in SPARC_. If you want to learn NASM-syntax x86 assembly, find a resource that teaches that. – Michael Nov 07 '18 at 09:02
  • @Michael I am well aware that "y = x + y" wouldn't work and needs to be rewritten to "mov eax, x| mov ebx, y| add eax, ebx|" what I want to know, since I can't find this, is how to write the "call subr(a,b)" in Assembly – Martin Nov 07 '18 at 09:05
  • 2
    Ok, so like I said, find a resource that teaches NASM-syntax x86 assembly if that's what you want to learn. The document you linked to is pretty much useless for that purpose (I didn't see a single line of NASM-syntax code in there). – Michael Nov 07 '18 at 09:11
  • 1
    It takes multiple instructions to set up args and then call a function, and no you wouldn't use `mov eax, x`, because `x` is a function arg (automatic storage), not static storage, so it won't have a symbol name. If you want examples of calling functions, look at compiler generated code. – Peter Cordes Nov 07 '18 at 09:12
  • @PeterCordes I obv would have declared x and y as a variable in the data section – Martin Nov 07 '18 at 09:14
  • If you did that, then it would correspond to C or pseudocode that declared 2 globals, assigned to them in `main`, and modified them in `subr`. i.e. communicating through global variables, not function args. The signature for `subr` would be `void subr(void)`, not `void subr(int x,int y)` like you want. That was the point of my previous comment. – Peter Cordes Nov 07 '18 at 09:17
  • See e.g. https://godbolt.org/z/hOTxIj This is MASM syntax, but it should give you an idea of the steps involved. – Michael Nov 07 '18 at 09:26
  • What operating system are you programming for? Is this 32 bit or 64 bit code? – fuz Nov 07 '18 at 09:41

0 Answers0