0

Could you please post an example in assembly language that uses functions with parameters. Something simple, like function that returns a sum of two elements.

Couldn't google any example that is simple enough.

ADDED:

.model small 
.data

.stack  320h 
.code   
    extrn  writer:near

    add_numbers PROC
        ARG number1:WORD
        ARG number2:WORD

        MOV ax, number1
        MOV bx, number2
        ADD ax, bx
        CALL writer ; this procedure prints the contents of ax

        RET 
    add_numbers ENDP

    .startup
    PUSH 1
    PUSH 2
    CALL add_numbers ; instead of 3 it prints -11602
    call writer ; instead of 3 it prints 0
.EXIT
    END
Sergey
  • 47,222
  • 25
  • 87
  • 129

1 Answers1

2

That would depend on the version of TASM you're using. On modern ones you can write something like:

add_numbers PROC
    ARG number1:DWORD
    ARG number2:DWORD

    MOV eax, [number1]
    MOV ebx, [number2]
    ADD eax, ebx
    RET
add_numbers ENDP
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • I'm using Turbo Assembler 4.1. Your example didn't compile because of register names. Replaced eax with ax, ebx with bx and I still get an error about the line "MOV eax, [number1]": Operand types do not match. – Sergey Dec 10 '11 at 10:45
  • 1
    Changed DWORD to WORD and it worked. But how can I now call this procedure? – Sergey Dec 10 '11 at 10:47
  • 1
    Use the `CALL` instruction: `PUSH , PUSH , CALL add_numbers`. The return value will be in `ax`. – Frédéric Hamidi Dec 10 '11 at 10:54
  • Tried this, but something still doesn't work... I added my code to the question. – Sergey Dec 10 '11 at 11:07
  • @sergey: you need to learn to use a debugger so that you can single-step through the code – Paul R Dec 10 '11 at 11:18
  • Agreed, using a debugger here would be way more helpful than calling an external printing procedure. Search for Turbo Debugger. – blaze Jan 29 '13 at 00:50