Difference between revisions of "CSC231 Homework 6 2017"

From dftwiki3
Jump to: navigation, search
(Problem Statement)
Line 10: Line 10:
 
<br />
 
<br />
 
Write an assembly language program similar to the one with work out in class, but with a few differences, listed below.  You can find the program we wrote in class [[CSC231_Developing_the_Game_of_Life_in_Assembly | here]].
 
Write an assembly language program similar to the one with work out in class, but with a few differences, listed below.  You can find the program we wrote in class [[CSC231_Developing_the_Game_of_Life_in_Assembly | here]].
 +
<br />
 +
* Your program will use an ''include'' file to get the array dish, which contains the initial 1-line generation of cells. 
 +
* Your program will not use a single '''loop''' instruction, and will implement all the loops using '''cmp''' and '''jump''' instructions (conditionals and unconditional jumps will be used).
 +
<br />
 +
==Using an Include File==
 +
<br />
 +
The concept is simple: you put part of the code into a separate file, and you use an "''%include''"  statement inside the main program to tell nasm that it should read the contents of the include file as part of this program.
 +
<br />
 +
The code below illustrates how this is done.  We have two files:
 +
; '''GameOfLifeDemo.asm'''
 +
: That's the main program.  It contains the data section and the code section.  In the data section is a statement ''%include "dishArray.inc'' that instructs nasm to read this other file
 +
; '''dishArray.inc'''
 +
: This file contains the definition of the '''dish''' array, and of the constant '''N'''.
 +
<br />
 +
===demo.asm===
 +
<br />
 +
::<source lang="asm" highlight="23">
 +
;;; ; demo.asm
 +
;;; ; Authors: D. Thiebaut and CSC231 Class
 +
;;; ;
 +
;;; ; Demo program illustrating how to
 +
;;; ; use an include file to feed the dish
 +
;;; ; array to the program.
 +
;;; ;
 +
;;; ; to assemble and run:
 +
;;; ;
 +
;;; ;    nasm -f elf -F  stabs demo.asm
 +
;;; ;    ld -melf_i386 -o demo demo.o
 +
;;; ;    ./demo
 +
;;; ; -------------------------------------------------------------------
 +
 +
 +
;;;  ------------------------------------------------------------
 +
;;;  data areas
 +
;;;  ------------------------------------------------------------
 +
 +
        section .data
 +
;;; the current dish with its cells.  We use 0 and 1 to
 +
;;; indicate dead and live cells, respectively.
 +
%include "dishArray.inc"
 +
 +
;;; 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
 +
 +
generation dd 0 ; save ecx from for generation loop
 +
i dd 0 ; used by for loops
 +
 +
;;;  ------------------------------------------------------------
 +
;;;  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 '!'
 +
 +
;;; for i in range( N ):
 +
mov esi, dish
 +
mov edi, dishP
 +
 +
mov dword[i], 0
 +
forPrint: cmp dword[i], N
 +
jge forPrintEnd
 +
 +
mov al, byte[esi]
 +
add al, ' ' ;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
 +
 +
;; i++, loop back
 +
inc dword[i]
 +
jmp forPrint
 +
forPrintEnd:
 +
 +
;; 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()
 +
exit: mov    eax,1
 +
        mov    ebx,0
 +
        int    0x80 ; final system call
 +
 +
</source>
 
<br />
 
<br />

Revision as of 20:04, 1 April 2017

--D. Thiebaut (talk) 18:56, 1 April 2017 (EDT)


Game Of Life Without Loop Instructions


For this assignment, you need to write a game of life program in assembly, and the challenge is to implement it without using the loop instruction, using only the cmp instruction and conditional jumps.


Problem Statement


Write an assembly language program similar to the one with work out in class, but with a few differences, listed below. You can find the program we wrote in class here.

  • Your program will use an include file to get the array dish, which contains the initial 1-line generation of cells.
  • Your program will not use a single loop instruction, and will implement all the loops using cmp and jump instructions (conditionals and unconditional jumps will be used).


Using an Include File


The concept is simple: you put part of the code into a separate file, and you use an "%include" statement inside the main program to tell nasm that it should read the contents of the include file as part of this program.
The code below illustrates how this is done. We have two files:

GameOfLifeDemo.asm
That's the main program. It contains the data section and the code section. In the data section is a statement %include "dishArray.inc that instructs nasm to read this other file
dishArray.inc
This file contains the definition of the dish array, and of the constant N.


demo.asm


;;; ; demo.asm
;;; ; Authors: D. Thiebaut and CSC231 Class
;;; ;
;;; ; Demo program illustrating how to 
;;; ; use an include file to feed the dish
;;; ; array to the program.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ;     nasm -f elf -F  stabs demo.asm
;;; ;     ld -melf_i386 -o demo demo.o
;;; ;     ./demo
;;; ; -------------------------------------------------------------------


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

	        section .data
;;; the current dish with its cells.  We use 0 and 1 to
;;; indicate dead and live cells, respectively.
%include	"dishArray.inc"

;;; 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
	
generation	dd	0			; save ecx from for generation loop
i		dd	0			; used by for loops
	
;;;  ------------------------------------------------------------
;;;  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 '!'

;;; for i in range( N ):
		mov	esi, dish 
		mov	edi, dishP

		mov	dword[i], 0
forPrint:	cmp	dword[i], N
		jge	forPrintEnd
	
		mov	al, byte[esi]
		add	al, ' '			;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

		;; i++, loop back
		inc	dword[i]
		jmp	forPrint
forPrintEnd:	
	
		;; 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()
exit:		mov     eax,1
	        mov     ebx,0
	        int     0x80	; final system call