2-Dimensional Array

This example uses a 2D Array to map out coordinates within the array and print it to the screen. Use a, s, d, and w keys to navigate the character throughout the 10×10 map. I couldn’t think of another way to clear the screen with each movement at the moment so I made it write newline characters to the next page, so there will probably be a slight lag especially when holding down the keys instead of pressing them one at a time.

;Assemble with FASM
org 100h
use16

main:
   push bp
   mov bp,sp
   sub sp,16

   mov word [bp-4],0       ;Local variable X
   mov word [bp-2],0       ;Local variable Y

   mov bx,hgrid            ;bx points to beginning of 2DArrayStruct
   mov word [bx],map       ;2DArrayStruct.hgrid = &map
   add bx,2
   mov word [bx],10        ;2DArrayStruct.height = 10
   add bx,2
   mov word [bx],10        ;2DArrayStruct.width = 10

   mov word [bp-16],hgrid  ;Use address of 2DArrayStruct as arg
   mov word [bp-14],0      ;X = 0
   mov word [bp-12],0      ;Y = 0
   mov byte [bp-10],41h    ;Character to set
   call setmap             ;Set coordinate

   mov word [bp-16],hgrid
   call printmap           ;printmap(2DArrayStruct);

   W1:
   mov word [bp-16],hgrid
   mov ax,word [bp-4]
   mov word [bp-14],ax
   mov ax,word [bp-2]
   mov word [bp-12],ax
   mov byte [bp-10],2eh
   call setmap             ;map[x][y]='.';

   mov ah,08               ;Read character input
   int 21h                 ;Character read stored in AL
   S1:                     ;Switch AL
      cmp al,97    ;'a'
      je S1C1
      cmp al,115   ;'s'
      je S1C2
      cmp al,100   ;'d'
      je S1C3
      cmp al,119   ;'w'
      je S1C4
      jmp S1E
      S1C1:                    ;case 'a'
         I1:
            mov ax,word [bp-2]
            cmp ax,0           ;if (y==1)
            jne I1C1
            mov ax,9           ;{y=9;}
            jmp I1E
            I1C1:
               dec ax          ;else {y--;}
         I1E:
         mov word [bp-2],ax
         jmp S1E               ;break;
      S1C2:                    ;case 's'
         I2:
            mov ax,word [bp-4]
            cmp ax,9           ;if (x==9)
            jne I2C1
            mov ax,0           ;{x=0}
            jmp I2E
            I2C1:
               inc ax          ;else {x++;}
         I2E:
         mov word [bp-4],ax
         jmp S1E               ;break;
      S1C3:
         I3:
            mov ax,word [bp-2]
            cmp ax,9           ;if (y==9)
            jne I3C1
            mov ax,0           ;{y=0}
            jmp I3E
            I3C1:
               inc ax          ;else {y++;}
         I3E:
         mov word [bp-2],ax
         jmp S1E               ;break;
      S1C4:
         I4:
            mov ax,word [bp-4]
            cmp ax,0           ;if (x==0)
            jne I4C1
            mov ax,9           ;{x=9;}
            jmp I4E
            I4C1:
               dec ax          ;else {x--;}
         I4E:
         mov word [bp-4],ax
   S1E:
   mov word [bp-16],hgrid
   mov ax,word [bp-4]
   mov word [bp-14],ax
   mov ax,word [bp-2]
   mov word [bp-12],ax
   mov byte [bp-10],41h
   call setmap                 ;setmap(2DArrayStruct,x,y,'A');

   mov word [bp-16],hgrid
   call printmap

   mov ah,09
   mov dx,cls
   int 21h                     ;Clear screen (sort of)

   jmp W1

   mov ax,4c00h
   int 21h

;Display's 2D Array, new line for every increment
;in 1st dimensional index.
printmap:  ;Argument: &2DArray
   push bp
   mov bp,sp
   push ax
   push bx
   push cx
   push dx
   push si
   push di
   sub sp,4
   mov word [bp-14],0
   L1:
      mov ax,word [bp-14]
      mov si,word [bp+4]
      cmp ax,word [si+2]
      je LE1
      mov word [bp-16],0
      L2:
         mov ax,word [bp-16]
         mov si,word [bp+4]
         cmp ax,word [si+4]
         je LE2
         mov ax,word [bp-14]
         mov si,word [bp+4]
         mov cx,word [si+4]
         mul cx
         add ax,word [bp-16]
         mov si,word [bp+4]
         mov bx,word [si]
         add bx,ax
         mov dl,byte [bx]
         mov ah,02
         int 21h
         mov ax,word [bp-16]
         inc ax
         mov word [bp-16],ax
         jmp L2
      LE2:
      mov ah,02
      mov dl,0dh
      int 21h
      mov dl,0ah
      int 21h
      mov ax,word [bp-14]
      inc ax
      mov word [bp-14],ax
      jmp L1
   LE1:
   add esp,4
   pop di
   pop si
   pop dx
   pop cx
   pop bx
   pop ax
   mov sp,bp
   pop bp
ret

;Sets value of index in 2D Array
setmap:      ;Arguments: &2DArrayStruct, word row, word column, byte char
   push bp
   mov bp,sp
   push ax
   push bx
   push cx
   push dx
   push si
   push di
   mov si,word [bp+4]
   mov ax,word [bp+6]
   mov cx,word [si+4]
   mul cx
   add ax,word [bp+8]
   mov bx,word [si]
   add bx,ax
   mov dl,byte [bp+10]
   mov byte [bx],dl
   pop di
   pop si
   pop dx
   pop cx
   pop bx
   pop ax
   mov sp,bp
   pop bp
ret


;Our 2DArray Structure {
hgrid dw 0   ;Pointer to array
height dw 0  ;Length of dimensions
width dw 0   ;Length of elements in each dimension
;}

;Our actual 2DArray:
map db "..........",\
       "..........",\
       "..........",\
       "..........",\
       "..........",\
       "..........",\
       "..........",\
       "..........",\
       "..........",\
       ".........."

;Used to write to screen throughout the next page
;for a clear screen effect
cls db 0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,24h

Here’s a similar program in C: http://pastebin.com/biwUSu3p

This batch file will automatically build the com file with debug.exe:

@echo off
REM Batch file created by Jakash3's mkdbg tool
REM For use on 32-bit Windows OS only
(
echo e 0100 55 89 E5 83 EC 10 C7 46 FC 00 00 C7 46 FE 00 00 
echo e 0110 BB 85 02 C7 07 8B 02 83 C3 02 C7 07 0A 00 83 C3 
echo e 0120 02 C7 07 0A 00 C7 46 F0 85 02 C7 46 F2 00 00 C7 
echo e 0130 46 F4 00 00 C6 46 F6 41 E8 20 01 C7 46 F0 85 02 
echo e 0140 E8 A7 00 C7 46 F0 85 02 8B 46 FC 89 46 F2 8B 46 
echo e 0150 FE 89 46 F4 C6 46 F6 2E E8 00 01 B4 08 CD 21 3C 
echo e 0160 61 74 0E 3C 73 74 1D 3C 64 74 2C 3C 77 74 3B EB 
echo e 0170 4A 8B 46 FE 83 F8 00 75 05 B8 09 00 EB 01 48 89 
echo e 0180 46 FE EB 37 8B 46 FC 83 F8 09 75 05 B8 00 00 EB 
echo e 0190 01 40 89 46 FC EB 24 8B 46 FE 83 F8 09 75 05 B8 
echo e 01A0 00 00 EB 01 40 89 46 FE EB 11 8B 46 FC 83 F8 00 
echo e 01B0 75 05 B8 09 00 EB 01 48 89 46 FC C7 46 F0 85 02 
echo e 01C0 8B 46 FC 89 46 F2 8B 46 FE 89 46 F4 C6 46 F6 41 
echo e 01D0 E8 88 00 C7 46 F0 85 02 E8 0F 00 B4 09 BA EF 02 
echo e 01E0 CD 21 E9 5E FF B8 00 4C CD 21 55 89 E5 50 53 51 
echo e 01F0 52 56 57 83 EC 04 C7 46 F2 00 00 8B 46 F2 8B 76 
echo e 0200 04 3B 44 02 74 47 C7 46 F0 00 00 8B 46 F0 8B 76 
echo e 0210 04 3B 44 04 74 24 8B 46 F2 8B 76 04 8B 4C 04 F7 
echo e 0220 E1 03 46 F0 8B 76 04 8B 1C 01 C3 8A 17 B4 02 CD 
echo e 0230 21 8B 46 F0 40 89 46 F0 EB D1 B4 02 B2 0D CD 21 
echo e 0240 B2 0A CD 21 8B 46 F2 40 89 46 F2 EB AE 66 83 C4 
echo e 0250 04 5F 5E 5A 59 5B 58 89 EC 5D C3 55 89 E5 50 53 
echo e 0260 51 52 56 57 8B 76 04 8B 46 06 8B 4C 04 F7 E1 03 
echo e 0270 46 08 8B 1C 01 C3 8A 56 0A 88 17 5F 5E 5A 59 5B 
echo e 0280 58 89 EC 5D C3 00 00 00 00 00 00 2E 2E 2E 2E 2E 
echo e 0290 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 
echo e 02A0 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 
echo e 02B0 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 
echo e 02C0 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 
echo e 02D0 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 
echo e 02E0 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 0D 
echo e 02F0 0A 0D 0A 0D 0A 0D 0A 0D 0A 0D 0A 0D 0A 0D 0A 0D 
echo e 0300 0A 0D 0A 0D 0A 0D 0A 0D 0A 0D 0A 24 
echo r cx
echo 020C
echo n map.com
echo w
echo q
)>temp.dbg
debug<temp.dbg>nul
del temp.dbg
echo map.com has been created in the current directory
pause

2 thoughts on “2-Dimensional Array

  1. Pingback: X2Q

  2. Pingback: X2Q

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: