NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: magnusbbc on June 10, 2016, 07:52:39 PM
-
Hi everyone,
I'm new at the forum and at assembler programming.
I have been using Dr Paul Carter's book (link (http://www.drpaulcarter.com/pcasm/)) to help me learn assembly. To write, build, and debug NASM I have been using the SASM (http://dman95.github.io/SASM/english.html) IDE. However when I built the following code:
%include "asm_io.inc"
segment .data
prompt1 db "Enter a number: ", 0 ; don't forget nul terminator
prompt2 db "Enter another number: ", 0
outmsg1 db "You entered ", 0
outmsg2 db " and ", 0
outmsg3 db ", the sum of these is ", 0
segment .bss
input1 resd 1
input2 resd 1
segment .text
global _main
_main:
enter 0,0 ; setup routine
pusha
mov eax, prompt1 ; print out prompt
call print_string
call read_int ; read integer
mov [input1], eax ; store into input1
mov eax, prompt2 ; print out prompt
call print_string
call read_int ; read integer
mov [input2], eax ; store into input2
mov eax, [input1] ; eax = dword at input1
add eax, [input2] ; eax += dword at input2
mov ebx, eax ; ebx = eax
dump_regs 1 ; dump out register values
dump_mem 2, outmsg1, 1 ; dump out memory
;
; next print out result message as series of steps
;
mov eax, outmsg1
call print_string ; print out first message
mov eax, [input1]
call print_int ; print out input1
mov eax, outmsg2
call print_string ; print out second message
mov eax, [input2]
call print_int ; print out input2
mov eax, outmsg3
call print_string ; print out third message
mov eax, ebx
call print_int ; print out sum (ebx)
call print_nl ; print new-line
popa
mov eax, 0 ; return back to C
leave
ret
I get this error:
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0xb): undefined reference to `print_string'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x10): undefined reference to `read_int'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x1f): undefined reference to `print_string'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x24): undefined reference to `read_int'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x3d): undefined reference to `sub_dump_regs'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x4b): undefined reference to `sub_dump_mem'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x55): undefined reference to `print_string'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x5f): undefined reference to `print_int'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x69): undefined reference to `print_string'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x73): undefined reference to `print_int'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x7d): undefined reference to `print_string'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x84): undefined reference to `print_int'
C:\Users\Chris\AppData\Local\Temp\SASM\program.o:C:\Users\Chris\Ap:(.text+0x89): undefined reference to `print_nl'
From reading the previous post on this issue (Link (http://forum.nasm.us/index.php?topic=2203.0)) I am of understanding that I have to somehow link the asm_io.obj , however I don't know how to do that - I think I have to change some settings within the settings window:
(http://i.imgur.com/mPe2htf.png)
any help is greatly appreciated.
Thanks in advance.
-
Hi magnusbbc,
Welcome to the forum!
The way Dr. Carter's stuff is "supposed" to be linked involves "driver.c" - a stub of C code which simply calls "asm_main", and your code will include:
global asm_main
asm_main:
However, your "first.asm" starts with:
global _main
_main:
I think this will work, simply bypassing "driver.c" - but it won't match exactly what it says in the book. Change it to "asm_main" and add "driver.c" to the "linker options" if you care.
As a one-time job, you will need to assemble "asm_io.asm" to "asm_io.obj":
nasm -f win32 -d COFF_TYPE asm_io.asm
then add "asm_io.obj" to the "linker options". Add "driver.c" too, if you're going to do it that way.
I see "$MACROS.OBJ$". I'm not sure what that refers to. You may want to remove it, if SASM complains about not being able to find it. May not hurt to leave it there.
Try something like:
linker options: $PROGRAM.OBJ$ asm_io.obj -o $PROGRAM% -g -m32
or perhaps:
linker options: driver.c $PROGRAM.OBJ$ asm_io.obj -o $PROGRAM% -g -m32
and see if that clears up the errors. You're on the right track, I think. I'm not actually familiar with SASM, but I think I see what it does...
Best,
Frank
-
Thanks a lot for the help Frank
When I try to remove
linker options: $PROGRAM.OBJ$ $MACRO.OBJ$ -g -o $PROGRAM$ -m32
I get the following error:
[20:46:03] Warning! Errors have occurred in the build:
gcc.exe: error: : No such file or directory
gcc.exe: fatal error: no input files
compilation terminated.
I tried both appending and substituting
$PROGRAM.OBJ$ asm_io.obj -o $PROGRAM% -g -m32
to the linker options, however in both cases sasm returns the following error:
[20:47:25] Warning! Errors have occurred in the build:
gcc.exe: error: asm_io.obj: No such file or directory
I have tried placing the asm_io.obj file in both the gcc.exe directory, aswell as the asm_io.inc directory.
-
I should have mentioned that "asm_io.obj" (it is called ".obj", not ".o", right?) needs to be where SASM can find it - same directory as "first.asm" should work(?). I really don't know how SASM arranges things...
Courage!
Frank
-
Thanks a lot for the help frank, i tried placing "asm_io.obj" in the "first.asm" directory, unfortunatly I still receive the same error. Would you happen to know of a different NASM IDE that would make it easier to link?
-
I usually work from the command line. Gotta type all that crap in once, but then just hit "up-arrow" to repeat it. I've used Jeff Owens' "asmide". It has a great built-in help system covering the system calls, but it's for Linux and won't do you any good. You could try using the full path: "C:\Program Files\pcasm\asm_io.obj" or whatever it is. You may need to do the same with "driver.c" if you're using it. It may help to use the "short name" (which is the "true name") "Progra~1" instead of the name with the space in it. A program is liable to interpret that as two options, "C:\Program" and "Files\..." - and not find either of them. Once you find the "trick" to this it will probably seem obvious. Something you'd have to go through with any IDE.
Best,
Frank