flat assembler
Message board for the users of flat assembler.

Index > DOS > String arrays

Author
Thread Post new topic Reply to topic
flash



Joined: 11 Mar 2006
Posts: 55
Location: Cuba
flash
Hi, I am testing for data structures in assembler in order to facilitate some operation over data like char strings or even structures (by the way, my fasm version 1.67.3 does not recognize struc nor struct directives. Why? Sad )
Any way I make some kind of array "mannagement" or so... Here is the code Can anyone tell me how to write a macro to covert a list of variable lenght of words to the same but with zero terminated string? It could facilitate the writing of strings arrays.
And... How can be implemented some method to access whith the same delay each element at the array?
Code:
format  MZ
include 'fasmDOSlib.asm'
entry codigo:START
segment datos
        meses           DB 'January',0,\
                           'February',0,\
                           'March',0,\
                           'April',0,\
                           'May',0,\
                           'June',0,\
                           'July',0,\
                           'August',0,\
                           'September',0,\
                           'October',0,\
                           'November',0,\
                           'December',0
        msg0             DB 'Element index to search(take care with boundaries): $'
        msg1             DB 13,10,'The element $'
        msg2             DB ' have the value: $'
        indice           DD 0

segment codigo
         START: push    datos
                pop     DS
                Mode    3
                Print   msg0
                ReadInt [indice]
                Print   msg1
                BaseNToScr [indice],10
                Print   msg2
                mov     EAX,[indice]
                mov     SI,meses
                call    SearchArray
                KeyPress
                Exit

   SearchArray: cmp     AX,0
                jne     TESTNEXT
    SHOWSTRING: PutChar [SI]
                inc     SI
                cmp     [SI],byte 0
                je      ENDOFSTRING
                jmp     SHOWSTRING
      TESTNEXT: inc     SI
                cmp     [SI],byte 0
                jne     TESTNEXT
                dec     AX
                inc     SI
                jmp     SearchArray
   ENDOFSTRING: ret
    


Sory for extensive use of macros. I like this to take clarity over efficiency when creating new stuffs. At fasmDOSlib.asm you can get:

Code:
CH_ENTER        EQU 13

        MACRO   Mode modo
        {
                xor     AH,AH
                mov     AL,modo
                int     10h
        }

        MACRO   Print cadena
        {
                mov     AH,9
                mov     DX,cadena
                int     21h
        }

        MACRO   PutChar caracter
        {
                mov     AH,2
                mov     DL,caracter
                int     21h
        }

        MACRO   BaseNToScr numero,base
        {
                LOCAL   CICLO,EXTRAER,ESPECIAL,MOSTRAR
                mov     EAX,numero
                mov     EBX,base
                and     CX,0
         CICLO: and     EDX,0
                div     EBX
                push    DX
                inc     CX
                cmp     EAX,0
                jne     CICLO
       EXTRAER: pop     DX
                cmp     DL,9
                ja      ESPECIAL
                add     DL,30h
                jmp     MOSTRAR
      ESPECIAL: add     DL,37h
       MOSTRAR: mov     AH,2
                int     21h
                loop    EXTRAER
        }

        MACRO   ReadInt variable
        {
                LOCAL   NEXT,DATAENTRY,FIN
                and     variable,0
                mov     EBX,10
          NEXT: KeyPress
                cmp     AL,CH_ENTER
                je      FIN
                cmp     AL,'0'
                jb      NEXT
                cmp     AL,'9'
                ja      NEXT
                PutChar AL
                sub     AL,30h
                xor     AH,AH
                push    EAX
                and     EAX,0
                pop     AX
                xchg    variable,EAX
                mul     EBX
                add     variable,EAX
                jmp     NEXT
           FIN:
        }

        MACRO   KeyPress
        {
                mov     AH,07h
                int     21h
        }

        MACRO   Exit
        {
                mov     AX,4C00h
                int     21h
        }
    

_________________
i don't hate goto
Post 14 Jul 2007, 05:08
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
(adaptation of the example of fasm doc)
Code:
    macro strtbl name,[string]
     {
      common
  label name word
      forward
    local label
 dw label
      forward
   label dw string,0
     }

strtab meses, 'January',\ 
              'February',\ 
              'March',\ 
              'April',\ 
              'May',\ 
              'June',\ 
              'July',\ 
              'August',\ 
              'September',\ 
              'October',\ 
              'November',\ 
              'December' 
    


Then, to get a pointer to a month you do this
Code:
getMonthPointer: ; AX = month number (starting from zero)
  push bx 
  add  ax, ax
  mov  bx, ax 
  mov ax, [bx+meses]
  pop  bx 
  ret; AX = pointer to month    


It is still possible to make all string with the same length but I think this way is better. Ask if you want the fixed length string version too
Post 14 Jul 2007, 15:52
View user's profile Send private message Reply with quote
flash



Joined: 11 Mar 2006
Posts: 55
Location: Cuba
flash
ThankĀ“s, but your function does not work to me. As far as I understand, the getMonthPointer function must return the value of the lement addresses. But why the duplicated value of index? Are you assuming fixed size of 2bytes for each element. If I am not wrong, the code
Code:
    macro strtab name,[string] 
     { 
      common 
        label name word 
      forward 
        local label 
        dw label 
      forward 
        label dw string,0 
     }
    

Shoul store the address of each element befor it self and the "attached zero" Because of this I do not understand getMonthPointer
Question
Post 16 Jul 2007, 02:53
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Example
Code:
org $100

; Should print July
  mov     ax, 7-1
  call    getMonthPointer

  mov     dx, ax
  mov     ah, 9
  int     $21

  xor     ax, ax
  int     $16

  int     $20

getMonthPointer:        ; AX = month number (starting from zero)
  push    bx
  add     ax, ax
  mov     bx, ax
  mov     ax, [bx+meses]
  pop     bx
  ret                   ; AX = pointer to month

    macro strtbl name,[string]
     { 
      common 
        label name word 
      forward 
        local label 
        dw label 
      forward 
        label db string,'$'
     } 

strtbl meses, 'January',\
              'February',\  
              'March',\  
              'April',\  
              'May',\  
              'June',\  
              'July',\  
              'August',\  
              'September',\  
              'October',\  
              'November',\  
              'December'    

I modified the macros to finish with '$' instead of zero but I did it just to make the strings suitable for INT21/AH=09.

Try again, my previous post had some typo errors ("label dw string,0" instead of "label db string,0" and "strtab meses" instead of "strtbl meses").
Post 16 Jul 2007, 03:05
View user's profile Send private message 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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.