title: Running multi-module programs in NASM assembly language
body: <p>I am trying to run <em>main4.asm</em> with a subprogram found in a different module <em>sub4.asm</em>.</p>
  <p>However when I compile <em>main4.asm</em> by:</p>
  <pre class="lang-none prettyprint-override"><code>nasm -f win32 main4.asm,
  gcc -o main4 main4.obj driver.c asm_io.obj
  </code></pre>
  <p>I always get the error:</p>
  <blockquote>
  <p>main4.obj:main4.asm:(.text+0x11): undefined reference to 'get_int'
  main4.obj:main4.asm:(.text+0x32): undefined reference to 'print_sum'
  collect2.exe: error: ld returned 1 exit status</p>
  </blockquote>
  <p>How can I fix this error?</p>
  <p>These are my codes:</p>
  <p><strong>main4.asm</strong></p>
  <pre class="lang-none prettyprint-override"><code> %include &quot;asm_io.inc&quot;
  
   segment .data
  
            sum dd 0
  
   segment .bss
  
            input resd 1
  
   segment .text
  
            global  _asm_main
  
            extern  get_int, print_sum
  
   _asm_main:
            enter 0,0 ; setup routine
            pusha
            mov edx, 1 ; edx is ’i’ in pseudo-code
   while_loop:
            push edx ; save i on stack
            push dword input ; push address on input on stack
            call get_int
            add esp, 8 ; remove i and &amp;input from stack
            mov eax, [input]
            cmp eax, 0
            je end_while
            add [sum], eax ; sum += input
            inc edx
            jmp short while_loop
   end_while:
            push dword [sum] ; push value of sum onto stack
            call print_sum
            pop ecx ; remove [sum] from stack
            popa
            leave
            ret
  </code></pre>
  <p><strong>sub4.asm</strong></p>
  <pre class="lang-none prettyprint-override"><code> %include &quot;asm_io.inc&quot;
  
   segment .data
  
           prompt db &quot;) Enter an integer number (0 to quit): &quot;, 0
  
   segment .text
  
           global get_int, print_sum
  
   get_int:
           enter 0,0
           mov eax, [ebp + 12]
           call print_int
           mov eax, prompt
           call print_string
           call read_int
           mov ebx, [ebp + 8]
           mov [ebx], eax ; store input into memory
           leave
           ret ; jump back to caller
  
   segment .data
  
           result db &quot;The sum is &quot;, 0
  
   segment .text
  
   print_sum:
           enter 0,0
           mov eax, result
           call print_string
           mov eax, [ebp+8]
           call print_int
           call print_nl
           leave
           ret
  </code></pre>