News:

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

Linking programs

Started by veronicak5678, October 09, 2009, 02:33:44 AM

Previous topic - Next topic

dedndave

one thing i see is this....

RANDOM       PROC    FAR PUBLIC USES CX DX DS

RANDOM is defined as a far procedure, yet it is in the near code segment
this may not cause a problem, but it could
when a RET instruction is encountered in a proc far, a RETF is generated instead of RETN
that is ok if the CALL pushes the code segment

let me look at the proc a few minutes and see if i can find an invalid instruction

dedndave

ok - i think i see what is goofing you up
you push 2 word parameters onto the stack of the RANDOM procedure, but never pop them off
at the end of the RANDOM proc, use:

        RET     4

that will return, and discard 2 words from the stack

veronicak5678

OK, I changed the far to a near and inserted RET 4. Still getting an error.

Here is the new code for random :

   
;===================================================================
;                   RANDOM.ASM
;      r a n d o m   n u m b e r   g e n e r a t o r
;
; GENERATES PSEUDO-RANDOM INTEGERS IN THE RANGE LOWER TO UPPER
; INPUT:  TWO STACK PARAMETERS - LOWER AND UPPER ENDS OF RANGE
; OUTPUT: AX-REG CONTAINS RANDOM INTEGER
; CALLING SEQUENCE:     PUSH    <LOWER END OF RANGE>
;                       PUSH    <UPPER END OF RANGE>
;                       CALL    RANDOM
;===================================================================
           .MODEL  SMALL, BASIC
   .586
           .STACK 256
;===================================================================
; D A T A   S E G M E N T   D E F I N I T I O N
           .DATA
SEED       DW      ?                   ;SEED FOR RANDOM NUMBER GEN.
MULTIPLIER DW      25173               ;MULTIPLIER AND
ADDEND     DW      13849               ;ADDEND FOR MIXED
                                       ;LINEAR CONGRUENTIAL METHOD
UPPER    DW      9999
LOWER      DW      0
PUBLIC SEED
;===================================================================
; C O D E   S E G M E N T   D E F I N I T I O N
           .CODE   
           ASSUME  DS:DGROUP



RESEED     PROC    FAR PUBLIC
           MOV     AX,DGROUP    ;SET DS-REGISTER TO POINT
           MOV     DS,AX               ;TO LOCAL DATA SEGMENT

                                    ;SAVE REGISTERS (USES LIST)
           PUSHF                    ;SAVE FLAGS
   CMP BL,0
   JNE NOTZERO
   PUSH LOWER
   PUSH UPPER
   CALL RANDOM
   MOV  SEED, AX
NOTZERO:
   MOV SEED,555H
           POPF                        ;RESTORE FLAGS
                                       ;RESTORE REGISTERS (ASSEMBLER
                                       ;GENERATES INSTRUCTIONS TO
                                       ;RESTORE REGISTERS - USES)
           RET                         ;RETURN
RESEED     ENDP                     



RANDOM       PROC    NEAR PUBLIC USES CX DX DS
                                       ;FUNCTION RANDOM(LOWER,UPPER)
                                       ;SAVE REGISTERS (USES LIST)
           PUSHF                       ;SAVE FLAGS

;

           MOV     AH,0                ;     SEED = LOWER HALF OF
           INT     1AH                 ;            TIME OF DAY CLOCK
           MOV     SEED,DX

           MOV     AX,SEED             ;X = SEED * MULTIPLIER mod
           MUL     MULTIPLIER          ;                       65536
           ADD     AX,ADDEND           ;SEED = (X + ADDEND) mod 65536
           MOV     SEED,AX
           MOV     CX,UPPER            ;RANGE = UPPER - LOWER + 1
           SUB     CX,LOWER
           INC     CX
           MUL     CX                  ;RANDOM = (SEED*RANGE)/65536
           ADD     DX,LOWER            ;                    + LOWER
           MOV     AX,DX
           POPF                        ;RESTORE FLAGS
                                       ;RESTORE REGISTERS (ASSEMBLER
                                       ;   GENERATES INSTRUCTIONS TO
                                       ;   RESTORE REGISTERS - USES)
           RET     4                        ;RETURN (RANDOM)
RANDOM       ENDP                     ;END RAND
           END   RESEED   


and main :

   
;===================================================================
;                   MAIN.ASM
;       sets BL =1, AX = 555H
;       calls Reseed
; Calls Rand 100 times w/ lower=0 upper = 9999
; Prints values 10 numbers/line and 6 ch/number and right justify
; Counts low values (0-4999) and high values (5000-9999)
; Counts evens and odds
; Sets BL=0
; Calls Reseed
; Calls Rand 100 times w/ lower=0 upper = 9999
; Prints values 10 numbers/line and 6 ch/number and right justify
; Counts low values (0-4999) and high values (5000-9999)
; Counts evens and odds
; Prints statistics (#/200)
;
;===================================================================
                EXTERN   GETDEC:FAR
                EXTERN   NEWLINE:FAR
                EXTERN   PUTDEC:FAR
                EXTERN   PUTSTRNG:FAR
;==================================================================
           .MODEL  SMALL,BASIC
;===================================================================
   .STACK 256
;===================================================================
; D A T A   S E G M E N T   D E F I N I T I O N
            .FARDATA MAIN_DATA
HIGHCOUNT DW 0
LOWCOUNT DW 0
EVENCOUNT DW 0
ODDCOUNT DW 0
HIGHHEADER DB 'High numbers: '
LOWHEADER DB 'Low numbers: '
ODDHEADER DB 'Odd numbers: '
EVENHEADER DB 'Even numbers: '
;===================================================================
; C O D E   S E G M E N T   D E F I N I T I O N
;
           .CODE
   EXTERN  RANDOM : FAR, RESEED : FAR
           ASSUME  DS:NOTHING,ES:MAIN_DATA

MAIN       PROC
   MOV     AX,DGROUP           ;SET ES-REGISTER TO POINT TO
           MOV     ES,AX               ;CONSTANT DATA SEGMENT

   MOV    BL,1
   CALL    RESEED
           MOV     CX,100              ;   LOOP_COUNT = 100
NUM_LOOP:                              ;   REPEAT
           PUSH    CX                  ;      PUSH LOOP_COUNT


           MOV    AX,0                ;PUSH LOWER
           PUSH    AX             
           MOV     AX,0000             ;PUSH UPPER
           PUSH AX
   CALL RANDOM
   
   CALL    PUTDEC
   CALL    NEWLINE             ;DISPLAY NEWLINE CHAR.
   CMP    AX, 5000
   JGE    HIGH_SEQUENCE
   INC     LOWCOUNT
HIGH_SEQUENCE:
   INC HIGHCOUNT
           SUB     DX, DX
           MOV     BX, 2
           DIV     BX
           CMP     DX, 0
           JNE     ODDS
   INC    EVENCOUNT
ODDS:
   INC    ODDCOUNT         
           POP     CX                  ;      POP LOOP_COUNT
           INC     AX                  ;      NUMBER = NUMBER + 1
           LOOP    NUM_LOOP            ;      DECREMENT LOOP_COUNT
                                       ;   UNTIL LOOP_COUNT = 0



   ;MOV    0,BL
   ;CALL    RESEED

                LEA     DI, HIGHHEADER
                MOV     CX, 18
                CALL    PUTSTRNG
CALL    NEWLINE               
MAIN       ENDP                     
           END MAIN


Thanks again for all your time. I am pretty lost (obviously!)

dedndave

lol - now you have RESEED as far - don't think it was before

RESEED     PROC    FAR PUBLIC

ok - now i will try and assemble these
i take it you are making 2 OBJ files and linking them together ?

dedndave

you have to fix this, too (put them all at the top of the file)

EXTERN  RANDOM : FAR, RESEED : FAR

to build this, i need:

                EXTERN   GETDEC:FAR
                EXTERN   NEWLINE:FAR
                EXTERN   PUTDEC:FAR
                EXTERN   PUTSTRNG:FAR

dedndave

i am going to hook you up with a little tool i use for 16-bit code
it is similar to debug, but with more features - very helpful
place it in the masm32\bin folder

veronicak5678

The command I am using is

ml main.asm random.asm io.lib

What exactly is wrong with

EXTERN  RANDOM : FAR, RESEED : FAR ?

They need to be in a different place?

Also, I am using MASM 611. Is that going to make any difference?


dedndave

let's start by defining the right memory model
if you can get all your stuff into one segment, small is good
if you are required to use far data and/or far code, we need to use large or compact

small:   one code segment, one data segment
medium:  multiple code segments, one data segment
compact: one code segment, multiple data segments
large:   multiple code segments, multiple data segments

i want to try to assemble this, but i need the following proc's (or maybe i need io.lib)
GETDEC
NEWLINE
PUTDEC
PUTSTRNG

i am not sure this works: ml main.asm random.asm io.lib
i think you have to assemble them seperately and create different OBJ's
then, link the OBJ's together
i could be wrong about that

veronicak5678

So for the models, main should use compact and random should use small?

I am pretty sure the assemble command I am using is OK. It is in the book like that,a dnI have used it before. I would send you io.lib, but I don't know how to attach things to these messages here!

dedndave

no - they use the same model
what i was trying to do by mentioning that was to get you to choose a model - lol
you need to decide from the onset whether you are going to use multiple code/data segments
i would do the minimum required by the project
if they require you to use multiple data segments - so be it
if they do not require you to use multiple code segments, avoid that - lol
one code segment and multiple data segments is compact - use that in all the files if that fills the bill

the io.lib file
zip it and attach it at the bottom of the reply where it says Additional Options...

dedndave

if io.lib has far code - we need to use large
let me look at it and i can figure it out

veronicak5678

Here it is. Thanks so much for all your time! I have learned about as much this morning as I have in a month from my teacher.

dedndave

the externs in that library are far's
i think you said you were required to use multiple data segments
so - that means large model

dedndave

well - i assembled main and random seperately
then, i linked them together with io.lib using lnk563.exe
result: it executed, no errors, but >poof< nothing happened
let's see if we can make something happen - lol

veronicak5678

We were supposed to be using multiple data segments when rand and reseed were two separate files, but I gave up on that. Now I think I'll just use one file for them and one data segment.