News:

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

how access to arry values?

Started by HATEM, July 16, 2009, 11:39:44 AM

Previous topic - Next topic

HATEM

hello
i have an array and i want to put its values in a register BH the first and the second ...(in loop of course),how can i do this

array db 53H,53H,53H,54H
------
mov cx,4
go:
mov bh,array[cx];!!!!!!! here the problem?
loop go



ToutEnMasm

Quote
   mov ecx,3      ;0,1,2,3            3 is the four value
   mov byte ptr bl,[array+ecx]

FORTRANS

#2
Hello,

   In 16-bit mode, you cannot use CX as an addressing register.  You
must use BX or BP as a base register or SI and DI as index registers.


Array   DB      53H, 53H, 53H, 54H

        MOV     SI,OFFSET Array
        MOV     CX,4
go:
        MOV     BH,[SI]
        INC     SI
        LOOP    go


HTH,

Steve N.

HATEM

thank you for your answer
but i can't us it look at my program


msg db item1, item2, item3, item4
color   db 1EH,53H,53H,53H

L                 db              13;position of the first line where we print the menu
c                 db              0;to print the 4 items
s                 db              1;position of cursor
d                 db              1;Index for color array

------------------

start1:
cmp d,2
jne go
mov cl,4
mov d,cl
jmp go1
go:
cmp d,4
jne go1
mov cl,2
mov d,cl
go1:

print_menu:
mov ch,c
inc ch
mov c,ch

        mov   ah,06h           
        mov   al,01h
        MOV   BH,[d];here the problem
        mov   ch,l       
        inc   ch
        mov   l,ch
        mov   cl,14       
        mov   dh,l       
        mov   dl,7       
        int   10h

        mov   bh,0     
        mov   dh,l     
        mov   dl,7     
        mov   ah,02
        int   10h
        lea   dx,msg[c];here the problem
        mov   ah,9
        int   21h
cmp  d,4
jne  here
mov  cl,1
mov  d,cl
jmp  go2
here:
inc d
go2:
cmp  c,4
jne   print_menu

down:
mov cl,0
mov c,cl

mov cl,13
mov l,cl

mov  ah,01h
int  21h
cmp  AL,61h
JNE  UP1
CMP  s,4
JE   ELSE2
MOV  BH,s
add  bh,1
mov  s,bh
mov cl,s
mov d,cl

jmp start1
ELSE2:

MOV BH,1
mov s,bh
mov cl,s
mov d,cl

jmp  start1

UP1:


        mov   ah,4ch
        int   21h


i want to program a menu to use it after solve the problem use the letter 'a' because i don't use the arraws

MichaelW

What assembler are you using? For MASM "c" is a reserved word.

If the attributes are for your menu, since there are only two values why not just handle them as constants?

Instead of Interrupt 10h function 6 I would use function 9 Write Character And Attribute At Cursor Position to set the attribute for the character cells were the menu items will be displayed.

mov ah, 9
mov al, 32        ; use a space character
mov bh, 0         ; specify display page zero
mov bl, 17h       ; the attribute value
mov cx, 6         ; specify the number of cells to write
int 10h


Instead of Interrupt 21h function 1, I would use Interrupt 16h function 10h Get Enhanced Keystroke to read the keyboard, and I would use the scan code returned in AH to identify the key.

Since menu item captions normally have variable lengths, I would use a table of pointers and index the table, something like this:

ptrs  dw cap0, cap1, cap2   ; this creates an array of pointers to the captions
cap0  db "Item0",13,10,"$"
cap1  db "Item1",13,10,"$"
cap2  db "Item2",13,10,"$"
. . .
mov si, baseZeroItemNumber
shl si, 1   ; scale index to match size of table items
mov dx, ptrs[si]


eschew obfuscation

HATEM

thank you MichaelW for your help
this is the code

.model small
.stack 200h
.data
msg db 'item1$'
color   dw 1EH,53H,53H,53H

L       db              13;position of the first line where we print the menu
c       db              0 ;to print the 4 items
s       db              1 ;position of cursor
d       db              0 ;Index for color array

.code
MOV  AX,@DATA
MOV  DS,AX
 
start1:
cmp d,2
jne go
mov cl,4
mov d,cl
jmp go1
go:
cmp d,4
jne go1
mov cl,2
mov d,cl
go1:

print_menu:
mov ch,c
inc ch
mov c,ch
        mov   ah,06h           
        mov   al,01h

       mov bh, byte ptr  [color+d] ; how i put elements of color in BH the index is d

        mov   ch,l        ; upper line
        inc   ch
        mov   l,ch
        mov   cl,14       ; right line
        mov   dh,l        ; down line
        mov   dl,7        ; left line 
        int   10h

        mov   bh,0 
        mov   dh,l   
        mov   dl,7   
        mov   ah,02
        int   10h
        lea   dx,msg
        mov   ah,9
        int   21h
cmp  d,4
jne  here
mov  cl,1
mov  d,cl
jmp  go2
here:
inc d
go2:
cmp  c,4
jne   print_menu

down:
mov cl,0
mov c,cl

mov cl,13
mov l,cl

mov  ah,01h
int  21h
cmp  AL,61h
JNE  UP1
CMP  s,4
JE   ELSE2
MOV  BH,s
add  bh,1
mov  s,bh
mov cl,s
mov d,cl

jmp start1
ELSE2:

MOV BH,1
mov s,bh
mov cl,s
mov d,cl

jmp  start1

UP1:

MOV  Ah,4CH
INT  21h
END


my problem is i can't use variable 'd' to index elements of array color?

MichaelW

#6
Quotemy problem is i can't use variable 'd' to index elements of array color

To use the value of the variable d as part of an address, you must place its value in a base or index register. For example:

color dw 1EH,53H,53H,53H
d     dw 0

. . .

mov bx, d

; Access the first element of the array:

mov ax, color[bx]


To access elements other than the first, you can add a displacement to the base or index register, or you can adjust the value in the base or index register. For example:

mov ax, color[bx+2]     ; access second element

add bx, 2               ; adjust value by size of elements
mov ax, color[bx]       ; access second element

eschew obfuscation

HATEM

sorry MichaelW i ask a lot
the problem still  :( look at the code


.model small
.stack 200h
.data
msg db 'item1$'
color   dw 1EH,53H,53H,53H

L       db              13;position of the first line where we print the menu
c       db              0;to print the 4 items
s       db              1;position of cursor
d       db              1;Index for color array

.code
MOV  AX,@DATA
MOV  DS,AX

start1:
cmp d,2
jne go
mov cl,4
mov d,cl
jmp go1
go:
cmp d,4
jne go1
mov cl,2
mov d,cl
go1:

print_menu:
mov ch,c
inc ch
mov c,ch
        mov   ah,06h         
        mov   al,01h


mov cl,d
mov bh,[color + cl];  the problem here
        mov   ch,l       ; upper line
        inc   ch
        mov   l,ch
        mov   cl,14      ; right line
        mov   dh,l       ; down line
        mov   dl,7       ; left line
        int   10h

        mov   bh,0
        mov   dh,l 
        mov   dl,7 
        mov   ah,02
        int   10h
        lea   dx,msg
        mov   ah,9
        int   21h
cmp  d,4
jne  here
mov  cl,1
mov  d,cl
jmp  go2
here:
inc d
go2:
cmp  c,4
jne   print_menu

down:
mov cl,0
mov c,cl

mov cl,13
mov l,cl

mov  ah,01h
int  21h
cmp  AL,61h
JNE  UP1
CMP  s,4
JE   ELSE2
MOV  BH,s
add  bh,1
mov  s,bh
mov cl,s
mov d,cl

jmp start1
ELSE2:

MOV BH,1
mov s,bh
mov cl,s
mov d,cl

jmp  start1

UP1:

MOV  Ah,4CH
INT  21h
END

look the error of the compiler



mov bh,[color + cl]; what does it mean must be index or base register!!!!

MichaelW

Go back and read FORTRANS post:

http://www.masm32.com/board/index.php?topic=11868.msg89973#msg89973

In 16-bit code the only way that you can use a variable to index an array is to place the value of the variable in a base or index register. Even for 32-bit code you still must place the value of the variable in a register, but you are not restricted to a base or index register.

You have two choices for accessing the color array. This first is a direct memory operand where the index is a constant. Some examples:

mov ax, color       ; source address is color+0
mov ax, [color]     ; source address is color+0
mov ax, color+0     ; source address is color+0
mov ax, color[0]    ; source address is color+0

mov ax, color+2     ; source address is color+2
mov ax, color[2]    ; source address is color+2

mov ax, color+4     ; source address is color+4
mov ax, color[4]    ; source address is color+4


The second is an indirect memory operand where the address is calculated at runtime from the contents of one or more registers. In 16-bit code the registers can be a base register or an index register, or one of each. The operand can also include one or more displacements. Some examples:

mov ax, color[bx]      ; source address is color+bx
mov ax, color[bx+2]    ; source address is color+bx+2
mov ax, color[bx+si]   ; source address is color+bx+si
mov ax, color[bx+si+2] ; source address is color+bx+si+2


And there is another problem with your statement:

mov bh,[color + cl]

Since color is defined as a WORD array, the elements will not fit in a BYTE register. That is why in my examples I used a 16-bit register as the destination.

Also, in 16-bit code addresses are 16-bit values, so in general you should not store addresses or indexes in byte registers.
eschew obfuscation

HATEM

thank you for your help
i found a solution and it works

.model small
.stack 200h
.data
msg    db    'item1$'
c1    db    1EH,53H,53H,53H
c2    db    53H,1EH,53H,53H
c3    db    53H,53H,1EH,53H
c4    db    53H,53H,53H,1EH

z    db    0
l    db    13
h    db    0

.code
MOV  AX,@DATA
MOV  DS,AX


start:
mov    l,13
cmp    z,0
jne    _11
lea    si,c1
jmp    print_screen
_11:
cmp    z,1
jne    _12
lea    si,c2
jmp    print_screen
_12:
cmp    z,2
jne    _13
lea    si,c3
jmp    print_screen
_13:
lea    si,c4
print_screen:
mov    ah,6
mov    al,1
mov    bh,byte ptr [si]
inc    l
mov    ch,l
mov    cl,14
mov    dh,l
mov    dl,7
int    10h
mov    ah,2
mov    bh,0
mov    dh,l
mov    dl,7
int    10h
lea    dx,msg
mov    ah,9
int    21h
cmp    h,3
je    wait_Input
inc    si
inc    h
jmp    print_screen
wait_Input:
mov    ah,1
int    21h
cmp    al,61h
jne    exit
cmp    z,3
jne    _14
mov    z,0
mov    h,0
jmp    start
_14:
mov    h,0
inc    z
jmp    start
exit:
mov    ah,4ch
int    21h
end


i have a question

i want to ask the user to enter a number which is the index of table and then i print the element which the user refer to

.model small
.stack 200h
.data

T    db    66h,3,65h,1,90h;45h

.code
MOV  AX,@DATA
MOV  DS,AX


mov    ah,1
int    10h

mov  byte ptr T,AL

mov   dx,byte ptr T
mov    ah,2
int    21h

mov    ah,4ch
int    21h
end


how can i do it?