CSC231 Developing the Game of Life in Assembly

From dftwiki3
Revision as of 11:27, 27 March 2017 by Thiebaut (talk | contribs) (Python Program Developed in Class (3/27/17))
Jump to: navigation, search

--D. Thiebaut (talk) 12:25, 27 March 2017 (EDT)


Python Program Developed in Class (3/27/17)


Source


dish = [ ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', ' ', '#',
         ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#',
         ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#',
         ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#',
         ' ', '#', ' ', '#', ' ', '#', ' ', ' ', ' ', ' ', ' ' ]

newGen = [' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#',
         ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#',
         ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#',
         ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#',
         ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ' ]

N = len( dish )

# applies the rules of life to the array dish,
# and stores the result in the array newGen, which
# is returned.  N is the number of cells.
def life( dish, N ):
    newGen = [' '] * N

    # loop through all the cells
    for i in range( 1, N-1 ):
        # look at fate of cell
        neighbors = 0
        if dish[i-1] == '#': neighbors += 1
        if dish[i+1] == '#': neighbors += 1

        if neighbors == 1:
            newGen[i] = '#'
        else:
            newGen[i] = ' '
        
        
    return newGen


def main():
    global dish, newGen, N
    maxGen = 20

    print( "".join( dish ) )
    
    # loop through generations
    for generation in range( maxGen ):

        newGen = life( dish, N )
        # display new generation
        print( "".join( newGen ) )

        # future becomes the present
        dish = newGen

        
main()


Output


     # # # # # # # # # # # # # # # # # # # # #     
    #                                         #    
   # #                                       # #   
  #   #                                     #   #  
 # # # #                                   # # # # 
        #                                 #        
       # #                               # #       
      #   #                             #   #      
     # # # #                           # # # #     
    #       #                         #       #    
   # #     # #                       # #     # #   
  #   #   #   #                     #   #   #   #  
 # # # # # # # #                   # # # # # # # # 
                #                 #                
               # #               # #               
              #   #             #   #              
             # # # #           # # # #             
            #       #         #       #            
           # #     # #       # #     # #           
          #   #   #   #     #   #   #   #          
         # # # # # # # #   # # # # # # # #


Assembly


Note, at the time we created this program, we didn't have available to us tests or functions, which makes the code slightly more complicated than needed.

Version 1 (in progress)


Source


;;; ; GameOfLife.asm
;;; ; Authors: D. Thiebaut and CSC231 Class
;;; ;
;;; ; Implements a 1-dimensional version of 
;;; ; Conway's Game of life.
;;; ; Note that because we do not "know" yet
;;; ; how to test or to use functions, the code
;;; ; is not as efficient as it could be.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ;     nasm -f elf -F  stabs GameOfLife.asm
;;; ;     ld -melf_i386 -o GameOfLife GameOfLife.o
;;; ;     ./GameOfLife
;;; ; -------------------------------------------------------------------


;;;  ------------------------------------------------------------
;;;  data areas
;;;  ------------------------------------------------------------

	        section .data
;;; the current dish with its cells.  We use 0 and 1 to
;;; indicate dead and live cells, respectively.
dish            db      0,0,0,1,1,1,0,1,0,1,0,1,0,1
		db	0,1,0,1,0,1,0,1
		db	0,1,0,1,0,1,0,1
		db	0,1,0,1,0,1,0,1
		db	0,1,0,1,0,1,0,1
N		equ	$-dish			; The number of cells
						;  in the dish

;;; the new generation of cells.  That's the "future" of the
;;; cells in the dish.  We make it the same size as dish, and
;;; fill it with N dead cells.
	
newGen	        times N db 0
	
;;; dishP is a string that will contain ' ' or '!' for
;;; each 0 or 1 in dish.  
dishP		times N db ' '
		db	10			; add a \n at the end
						;  of dishP
	
maxGen		dd	20			; the max number of
						; generations
	
;;;  ------------------------------------------------------------
;;;  code area
;;;  ------------------------------------------------------------

	        section .text
	        global  _start

_start:
main:	

;;; print( dish )
		;; first we copy dish into dishP and at the same
		;; time replace 0 by ' ' and 1 by '!'
		mov	ecx, N			;for each byte in dish
		mov	esi, dish		;esi points to source
		mov	edi, dishP
forPrint:	mov	al, byte[esi]
		add	al, 32			;0 becomes ' ', 1 becomes '!'
		mov	byte[edi], al
		inc	esi			;esi points to next byte in dish
		inc	edi			;edi points to next byte in dispP
		loop	forPrint

		;; print dishP as a string.
		mov	eax, 4			;prints dishP to screen
		mov	ebx, 1
		mov	ecx, dishP 		;dishP address
		mov	edx, N+1		;+1 to include the \n
		int	0x80



;;;  exit()

	        mov     eax,1
	        mov     ebx,0
	        int     0x80	; final system call

Output


                # # # # # # # #                 
               #               #                
              # #             # #               
             #   #           #   #              
            # # # #         # # # #             
           #       #       #       #            
          # #     # #     # #     # #           
         #   #   #   #   #   #   #   #          
        # # # # # # # # # # # # # # # #         
       #                               #        
      # #                             # #       
     #   #                           #   #      
    # # # #                         # # # #     
   #       #                       #       #    
  # #     # #                     # #     # #   
 #   #   #   #                   #   #   #   #  
  # # # # # # #                 # # # # # # # # 
 #             #               #                
  #           # #             # #               
 # #         #   #           #   #              
    #       # # # #         # # # #