Skip to content

Instantly share code, notes, and snippets.

@b-adams
Created December 9, 2012 03:40
Show Gist options
  • Save b-adams/4243230 to your computer and use it in GitHub Desktop.
Save b-adams/4243230 to your computer and use it in GitHub Desktop.
cs225fa12 2d array passing and access

Key things to remember when working with a 2d-array:

  • In an NxN array, accessing foo[x][y] is essentially the same thing as accessing foo[N*x+y]
  • When accessing an array through a pointer on the stack (e.g. when you're in a function that's been passed a pointer to the array) you need SXF mode - (S)tack because it's on the stack, indue(X)ed because you want to do indexing, and de(F)erred because you've got a pointer to the contents instead of the contents themselves.

Below is a demo program. I've broken up each function into its own files: a .pep file for the callee's business, and a .h file for the CALLER's business. Here's the shell of the program into which the other stuff would be included:

-------------------------------------------------------------------------------
      Object
Addr  code   Symbol   Mnemon  Operand     Comment
-------------------------------------------------------------------------------
             ;Demonstration of working with a two-dimensional array
0000  160004          CALL    demo        
0003  00              STOP                

00B7                  .END                  
-------------------------------------------------------------------------------
;Function `demo` ****************************************
;void demo()
;{
; int row;
; int col;
; char grid[64];
; int index;
; for(index=0; index<64; index++)
; {
; grid[index] = 'a'+index;
; }
; for(row=0; row<8; row++)
; printf("\n");
; for(col=0; col<8; col++)
; showoff(row, col, grid);
;
; return;
;}
;Important constants
gridsize:.EQUATE 64 ;Size of the grid
roocsize:.EQUATE 8 ;Size of a row/column
;callee/local variable info
row: .EQUATE 0 ;local variable #2d
col: .EQUATE 2 ;local variable #2d
grid: .EQUATE 4 ;local 8x8 array #1c64a
index: .EQUATE 68 ;local variable #2d
dframe: .EQUATE 70 ;size of local frame
0004 24 demo: NOP0
0005 680046 SUBSP dframe,i ;allocate #index #grid #col #row
;(index=0;
0008 C00000 LDA 0,i
000B E30044 STA index,s ;
000E 24 dfors: NOP0 ;Demo FOR loop Start
;index<64
000F C30044 LDA index,s
0012 B00040 CPA gridsize,i
0015 0E0030 BRGE dfore
;grid[index] = 'a'+index;
;;Prepare to access [index] for a char-sized array
0018 CB0044 LDX index,s
;;Get right-hand value into accumulator
001B C00061 LDA 'a',i
001E 730044 ADDA index,s
;;Perform assignment
0021 F60004 STBYTEA grid,sx ;;Byte Array stored entirely on local stack: SX mode
;index++)
0024 C30044 LDA index,s
0027 700001 ADDA 1,i
002A E30044 STA index,s
002D 04000E BR dfors
0030 24 dfore: NOP0 ;Demo FOR loop End
; for(row=0; row<8; row++)
0031 C00000 LDA 0,i
0034 E30000 STA row,s
0037 24 rloops: NOP0
0038 C30000 LDA row,s
003B B00008 CPA roocsize,i
003E 0E0089 BRGE rloope
; printf("\n");
0041 50000A CHARO '\n',i
; for(col=0; col<8; col++)
0044 C00000 LDA 0,i
0047 E30002 STA col,s
004A 24 cloops: NOP0
004B C30002 LDA col,s
004E B00008 CPA roocsize,i
0051 0E007C BRGE cloope
; showoff(row, col, grid);
; Tricky bit: C doesn't store arrays globally OR locally.
; It just has a global or local POINTER to the array.
; Functions inputting arrays expect a pointer, so we need to find &grid and pass THAT...
0054 02 MOVSPA ;;A now has SP (pointer to row)
0055 700004 ADDA grid,i ;;A now has SP+grid (pointer to grid)
0058 E3FFFE STA GRINPUT,s ;;Preparing argument for function call
005B C30000 LDA row,s
005E E3FFFA STA ROWINPUT,s ;;Preparing argument for funcall
0061 C30002 LDA col,s
0064 E3FFFC STA COLINPUT,s ;;Preparing arg for funcall
0067 680006 SUBSP XFRAME,i ;;Allocate #grinput #colinput #rowinput
006A 16008E CALL showoff ; call function
006D 600006 ADDSP XFRAME,i ;;Deallocate #rowinput #colinput #grinput
;end row loop
0070 C30002 LDA col,s
0073 700001 ADDA 1,i
0076 E30002 STA col,s
0079 04004A BR cloops
007C 24 cloope: NOP0
;end row loop
007D C30000 LDA row,s
0080 700001 ADDA 1,i
0083 E30000 STA row,s
0086 040037 BR rloops
0089 24 rloope: NOP0
; return;
;}
008A 680046 SUBSP dframe,i ;deallocate #row #col #grid #index
008D 58 RET0
;Stuff that everybody ELSE cares about
XFRAME: .EQUATE 6 ;size of CALLER frame for params and retvals
ROWINPUT:.EQUATE -6 ;Top-most block gets biggest negative
COLINPUT:.EQUATE -4
GRINPUT: .EQUATE -2 ;Bottom-most block gets smallest negative
;Function `showoff` ****************************************
;void showoff(int rowinput, int colinput, char** grinput)
;{
; char capped;
; capped = grinput[row][col];
; printf("%c", capped);
; capped = toupper(capped);
; printf("%c", capped);
; return;
;}
;Stuff that the function implementer cares about
capped: .EQUATE 0 ;local variable #1c
iframe: .EQUATE 1 ;size of local variable frame (callee's responsibility)
;return address - two bytes starting at 1 - CALL instructions's responsibility
rowinput:.EQUATE 3 ;formal parameter #2d
colinput:.EQUATE 5 ;formal parameter #2d
grinput: .EQUATE 7 ;formal parameter #2h - IT'S AN ADDRESS!
008E 24 showoff: NOP0
008F 680001 SUBSP iframe,i ;allocate #capped
;capped = grinput[row][col];
;Need to figure out how far [row][col] is from where grinput starts
;That'd be 8*row+col, so that's what we need to load into the indeX register
0092 CB0003 LDX rowinput,s ;get row into A
0095 1D ASLX ;make that 2*row in A
0096 1D ASLX ;make that 4*row in A
0097 1D ASLX ;make that 8*row in A
0098 7B0005 ADDX colinput,s ;make that 8*row+col in A
;no more aslX, because this is an array of 1-byte characters, not 2-byte integers
009B D70007 LDBYTEA grinput,sxf ;Load 1-BYTE from an local (s) array (x) pointer (f)
009E F30000 STBYTEA capped,s
;printf("%c", capped);
00A1 530000 CHARO capped,s
; capped = toupper(capped);
andmask: .EQUATE 0x00DF ;for turning off the lower-case bit
00A4 D30000 LDBYTEA capped,s
00A7 9000DF ANDA andmask,i ;capitalize
00AA F30000 STBYTEA capped,s
;printf("%c", capped);
00AD 530000 CHARO capped,s
;printf(".");
00B0 50002E CHARO '.',i
00B3 600001 ADDSP iframe,i ;dellocate #capped
00B6 58 RET0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment