flat assembler
Message board for the users of flat assembler.

Index > DOS > (newbe) Problem printing array value

Author
Thread Post new topic Reply to topic
umen



Joined: 14 Aug 2005
Posts: 16
umen 14 Aug 2005, 19:12
Hello all
im trying to print the array value put with out success, it always prints me the first array member value
here is the code :

Code:
-----------------------------------------------------------
DSEG SEGMENT
THEARRAY DB '1000$','2000$','3000$','4000$','5000$'                                         
DSEG ENDS
SSEG SEGMENT STACK                             
DW  100H DUP(0)
SSEG ENDS
CSEG SEGMENT
ASSUME DS:DSEG,SS:SSEG,CS:CSEG
START:
MOV DI,0
 MOV DX,OFFSET THEARRAY[0]
   MOV AH,9
   INT 21H
   INC DI
 MOV DX,OFFSET THEARRAY[4]
   MOV AH,9
   INT 21H
   INC DI
 MOV DX,OFFSET THEARRAY[9]
   MOV AH,9
        INT 21H
 MOV DX,OFFSET THEARRAY[14]
   MOV AH,9
        INT 21H
    MOV AX,4C00H
    INT 21H
CSEG ENDS
END START
    
Post 14 Aug 2005, 19:12
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 15 Aug 2005, 20:09
arrays dont work that way in asm.. thats C your thinking of.

what you do is.. say you have an array of single bytes
Code:
array db 21, 40, 31    

if you want the address of the first element, do
Code:
mov dx, array    

for the address of the second element, you take the base address (array) and add the number of elements you want to skip.. so:
Code:
mov dx, array+1    
for the address of the second element.
If this array was an array of words though (2bytes each, then to access the address of element 2, you would do:
Code:
mov dx, array+2    
)

I hope that was kinda clear. Feel free to ask otherwise.
Post 15 Aug 2005, 20:09
View user's profile Send private message Reply with quote
umen



Joined: 14 Aug 2005
Posts: 16
umen 16 Aug 2005, 13:19
Ok first tnx for the reply .. this is confusing me a lot this simple array manipulation just don’t work in any way I try to print the element
Please.. please some one take this code and try to run it .. it is just don’t work. let me understand why...
Code:
------------------------------------------------------------------------
DSEG SEGMENT
THEARRAY DB 23,56,88,99                                  
DSEG ENDS
SSEG SEGMENT STACK                             
DW  100H DUP(0)
SSEG ENDS
CSEG SEGMENT
ASSUME DS:DSEG,SS:SSEG,CS:CSEG
START:
 MOV DX,THEARRAY
   MOV AH,9
   INT 21H
 MOV DX,THEARRAY+1
   MOV AH,9
   INT 21H
 MOV DX,THEARRAY+2
   MOV AH,9
   INT 21H
 MOV DX,THEARRAY+3
   MOV AH,9
        INT 21H
    MOV AX,4C00H
    INT 21H
CSEG ENDS
END START
------------------------------------------------------------------------
this one gives me this error: 
tst1.asm(10) : error A2070: invalid instruction operands
tst1.asm(13) : error A2070: invalid instruction operands
tst1.asm(16) : error A2070: invalid instruction operands
tst1.asm(19) : error A2070: invalid instruction operands
-----------------------------------------------------------------------
when i try this code : 

DSEG SEGMENT
THEARRAY DB 23,56,88,99                                        
DSEG ENDS
SSEG SEGMENT STACK                             
DW  100H DUP(0)
SSEG ENDS
CSEG SEGMENT
ASSUME DS:DSEG,SS:SSEG,CS:CSEG
START:
 MOV DX,OFFSET THEARRAY
   MOV AH,9
   INT 21H
 MOV DX,OFFSET THEARRAY+1
   MOV AH,9
   INT 21H
 MOV DX,OFFSET THEARRAY+2
   MOV AH,9
   INT 21H
 MOV DX,OFFSET THEARRAY+3
   MOV AH,9
   INT 21H
    MOV AX,4C00H
    INT 21H
CSEG ENDS
END START
-----------------------------------------------------------------------
    

the result is bench of crap on the screen ….


what is wrong here ? please help
thenks allot
Post 16 Aug 2005, 13:19
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 16 Aug 2005, 13:59
hi umen,
you have to make your own functions to use your arrays,

my example

Code:
org $100

push ds ; in case of exe file
push cs
pop ds

mov si,THEARRAY
mov bx,0
call printelement

mov si,THEARRAY
mov bx,1
call printelement

mov si,THEARRAY
mov bx,2
call printelement

mov si,THEARRAY
mov bx,3
call printelement

mov si,THEARRAY
mov bx,4
call printelement

mov si,THEARRAY
mov bx,5
call printelement

;waitkey;
xor ah,ah
int 16h

pop ds

MOV AX,4C00H
INT 21H

THEARRAY: db "Array element1*","$","Array Element2*","$","Array Element3*","$","Bonus element*","$",0
;THEARRAY: db "Array element1$","Array Element2$","Array Element3$","Bonus element$",0
;THEARRAY: db "helloszia$","sziaszia$","imallright$",0

printelement: ; proc print array element, in a null terminated array
              ; (ds:)si = parameter input, bx=element to print, 0 is first
or bx,bx
jz .write
.loop:
lodsb
or al,al ; out of range?
jz .error
cmp al,"$"
jne .loop
dec bx
jmp printelement
.error: ; display error message
mov dx,.ermsg
mov ah,9
int 21h
jmp .done
.write:
lodsb
or al,al
jz .error
dec si
mov dx,si
mov ah,9
int 21h
.done:
ret
.ermsg: db "out of range.*","$"
    
Post 16 Aug 2005, 13:59
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.