News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Program to sum two numbers

Started by blue, August 27, 2011, 09:52:50 AM

Previous topic - Next topic

blue

Hello everyone !

I'm trying to learn assembly language but there's a big problem because my first program doesn't assemble. (the hello world doesn't count :D). I've been figthing with this problem for over 24 hours and it's very frustrating.

It'a a program from "Introduction to 80x86 assembly language and computer architecture". The error is:

unresolved external symbol _itoaproc
unresolved external symbol _atoiproc
unresolved external symbol _dtoaproc
unresolved external symbol _atodproc
unresolved external symbol _inproc
unresolved external symbol _outproc
unresolved external symbol _WinMainCRTSStartup

The code is as follows:

; Example assembly language program -- adds two numbers
; Author:  R. Detmer
; Date:    revised 7/97

.386
.MODEL FLAT, stdcall
option casemap: none
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
INCLUDE \masm32\include\windows.inc ; windows file
INCLUDE \masm32\include\io.txt            ; header file for input/output
.data

cr      EQU     0dh     ; carriage return character
Lf      EQU     0ah     ; line feed

.STACK  4096            ; reserve 4096-byte stack

.DATA                   ; reserve storage for data
number1 DWORD   ?
number2 DWORD   ?
prompt1 BYTE    "Enter first number:  ", 0
prompt2 BYTE    "Enter second number:  ", 0
string  BYTE    40 DUP (?)
label1  BYTE    cr, Lf, "The sum is "
sum     BYTE    11 DUP (?)
        BYTE    cr, Lf, 0

.CODE                           ; start of main program code
_start:
        output  prompt1         ; prompt for first number
        input   string, 40      ; read ASCII characters
        atod    string          ; convert to integer
        mov     number1, eax    ; store in memory

        output  prompt2         ; repeat for second number
        input   string, 40
        atod    string
        mov     number2, eax
       
        mov     eax, number1    ; first number to EAX
        add     eax, number2    ; add second number
        dtoa    sum, eax        ; convert to ASCII characters
        output  label1          ; output label and sum

        INVOKE  ExitProcess, 0  ; exit with return code 0

PUBLIC _start                   ; make entry point public

END                             ; end of source code



; IO.H - header file for I/O macros
; 32-bit version for flat memory model
; R. Detmer last revised 8/2000
.NOLIST ; turn off listing
.386

EXTRN itoaproc:near32, atoiproc:near32
EXTRN dtoaproc:near32, atodproc:near32
EXTRN inproc:near32, outproc:near32

itoa MACRO dest,source,xtra ;; convert integer to ASCII string

IFB <source>
.ERR <missing operand(s) in ITOA>
EXITM
ENDIF

IFNB <xtra>
.ERR <extra operand(s) in ITOA>
EXITM
ENDIF

push ebx ;; save EBX
mov bx, source
push bx ;; source parameter
lea ebx,dest ;; destination address
push ebx ;; destination parameter
call itoaproc ;; call itoaproc(source,dest)
pop ebx ;; restore EBX
ENDM

atoi MACRO source,xtra ;; convert ASCII string to integer in AX
;; offset of terminating character in ESI

IFB <source>
.ERR <missing operand in ATOI>
EXITM
ENDIF

IFNB <xtra>
.ERR <extra operand(s) in ATOI>
EXITM
ENDIF
push ebx ;; save EBX
lea ebx,source ;; source address to EBX
push ebx ;; source parameter on stack
call atoiproc ;; call atoiproc(source)
pop ebx ;; parameter removed by ret
ENDM

dtoa MACRO dest,source,xtra ;; convert double to ASCII string

IFB <source>
.ERR <missing operand(s) in DTOA>
EXITM
ENDIF

IFNB <xtra>
.ERR <extra operand(s) in DTOA>
EXITM
ENDIF

push ebx ;; save EBX
mov ebx, source
push ebx ;; source parameter
lea ebx,dest ;; destination address
push ebx ;; destination parameter
call dtoaproc ;; call dtoaproc(source,dest)
pop ebx ;; restore EBX
ENDM

atod MACRO source,xtra ;; convert ASCII string to integer in EAX
;; offset of terminating character in ESI
IFB <source>
.ERR <missing operand in ATOD>
EXITM
ENDIF

IFNB <xtra>
.ERR <extra operand(s) in ATOD>
EXITM
ENDIF

lea eax,source ;; source address to EAX
push eax ;; source parameter on stack

call atodproc ;; call atodproc(source)
;; parameter removed by ret
ENDM

output MACRO string,xtra ;; display string
IFB <string>
.ERR <missing operand in OUTPUT>
EXITM
ENDIF
IFNB <xtra>
.ERR <extra operand(s) in OUTPUT>
EXITM
ENDIF

push eax ;; save EAX
lea eax,string ;; string address
push eax ;; string parameter on stack
call outproc ;; call outproc(string)
pop eax ;; restore EAX
ENDM

input MACRO dest,length,xtra ;; read string from keyboard

IFB <length>
.ERR <missing operand(s) in INPUT>
EXITM
ENDIF

IFNB <xtra>
.ERR <extra operand(s) in INPUT>
EXITM
ENDIF

push ebx ;; save EBX
lea ebx,dest ;; destination address
push ebx ;; dest parameter on stack
mov ebx,length ;; length of buffer
push ebx ;; length parameter on stack
call inproc ;; call inproc(dest,length)
pop ebx ;; restore EBX
ENDM

.NOLISTMACRO ; suppress macro expansion listings
.LIST ; begin listing






ToutEnMasm

Quote
unresolved external symbol _itoaproc
unresolved external symbol _atoiproc
unresolved external symbol _dtoaproc
unresolved external symbol _atodproc
unresolved external symbol _inproc
unresolved external symbol _outproc
unresolved external symbol _WinMainCRTSStartup
a library is missing            (includelib Untel.lib)
_WinMainCRTSStartup :
When you try to use the CRT library , the entry point of your source code is in the crt library (LIBCMTD.lib ...)
You must create  a proc named (there is further names) main,wmain ....  study  a sample who use it to have the correct name.
"_dtoaproc" .. look like some functions of the crt but aren't the same.
You have to find in which library they are.







dedndave

a while back, i played with a bubble sort that was supposed to assemble with Detmer's library
i never could get it to assemble - lol
as i recall, some of the other forum members did because they had the appropriate C compiler installed

if you want to make that work, use the forum search tool - terms like "Detmer" and "io.h" will probably get results

as far as i know, Detmer's book is the only place these libraries are used
this is similar to Kip Irvine's libraries and book
you may want to scrap all that and learn to write using the masm32 libraries   :U
there is no book to go with it, per se
however, you will find a lot of helpful information and example code
and, the forum members will be willing to help   :P

blue

Thank you for your answers. So this book is worthless because none of the examples are working. Bad news.
Do you know another book with working code ?

hutch--

Try the 21st century technique, install MASM32.  :bg
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

in the upper right corner of the forum page, you will find the download links

blue

@Hutch, are you ironical ?
Your answer doesn't help me. I think it was assumed that masm32 was installed in my computer as far as i got the errors above.

MASM32 is an editor and assembler I need some examples in order to learn something.

Thank you.

ToutEnMasm



Quote
MASM32 is an editor and assembler I need some examples in order to learn something.
Not only .A good start  is with the samples  in the \masm32\examples    directory.
There is also tools.

dedndave

there is much more to masm32 than an editor and an assembler   :bg

the editor is an add-on
the assembler is actually from microsoft - stick around - there are many other assemblers that may be substituted

masm32 is first and foremost a library of working routines and macros
these will save you a lot of work   :U

to get going, you can look in the masm32\help and masm32\examples folders
there are a number of other resources, as well

but, let's start with adding 2 values together
        INCLUDE    \masm32\include\masm32rt.inc

        .CODE

_main   PROC

        mov     eax,input(13,10," Enter first number: ")
        INVOKE  atodw,eax
        push    eax

        mov     eax,input("Enter second number: ")
        INVOKE  atodw,eax
        push    eax

        print   chr$("         The sum is: ")
        pop     eax
        pop     edx
        add     eax,edx
        print   ustr$(eax),13,10,13,10

        inkey
        exit

_main   ENDP

        END     _main


the program uses the "atodw" routine, as well as the following macros:
input, print, chr$, ustr$, inkey, exit

in the masm32\m32lib folder, you can find the source code for atodw
in the masm32\macros\macros.asm file, you can find the macros

functions are documented in masm32\help\masmlib.chm
macros are documented in masm32\help\hlhelp.chm

once you have gotten your feet wet with console stuff,
let us know and we can point you at some windows stuff   :bg

GregL

blue,

I recommend you use MASM32 also, but if you really want to go through the book, you have only included the macros in io.txt, you also need to include the library (.lib file) with the procedures _itoaproc, _atoiproc etc. that the macros use.

[edit] I got out my copy of this book and the procedures are in io.asm and the file io.obj is the assembled version. So, you could include io.asm or you could link in io.obj.  A .lib file isn't provided, but you could make one.


MichaelW

blue,

The "unresolved external symbol _WinMainCRTStartup" indicates two things.

The first is that you are specifying /SUBSYSTEM:WINDOWS on the linker command line. The example is a console app, so you should be specifying /SUBSYSTEM:CONSOLE.

The second is that your example source does not specify an entry point address, so the linker goes looking for one, either _WinMainCRTStartup, or for a console app _mainCRTStartup. You can correct this by changing the END directive to:

END _start                      ; end of source code

Or by specifying the entry point on the linker command line.

Here is the batch file that I used to build and test the example:

@echo off
set file="example"
set lib=c:\masm32\lib
set path=c:\masm32\bin;%path%
if exist %file%.obj del %file%.obj
if exist %file%.exe del %file%.exe
if exist io.obj del io.obj
ml /c /coff io.asm
pause
ml /c /coff %file%.asm
pause
Link /SUBSYSTEM:CONSOLE /ENTRY:start kernel32.lib %file%.obj io.obj
pause
%file%
pause


eschew obfuscation

blue

I'm very grateful for your help.

@GregL, @Michael do you have the file io.asm ? I have only io.h.



dedndave

here is the stuff...

ToutEnMasm

Quote
@GregL, @Michael do you have the file io.asm ? I have only io.h.
Windows sdk and CRT translted here
Quote
http://www.masm32.com/board/index.php?topic=5428.msg40500#msg40500
/quote]

blue

@dedndave, how do I assemble the files from the archive windows.zip ? Visual Studio ?