Difference between revisions of "CSC231 Code Snippets"

From dftwiki3
Jump to: navigation, search
(Created page with "=Examples= <br /> <br /> ::<source lang="asm"> ;;; examples.asm ;;; D. Thiebaut ;;; ;;; A menagery of code snippets illustrating ;;; frequent constructs (for loops, etc) ;;; ...")
 
 
Line 20: Line 20:
 
array dd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
 
array dd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
 
N equ ($-array)/4
 
N equ ($-array)/4
 +
i dd 0
 +
x dd 0
 +
y dd 0
 +
 
 
 
section .text
 
section .text
Line 25: Line 29:
 
global _start
 
global _start
 
_start:
 
_start:
 +
 +
;;; -----------------------------------------------
 +
;;; -----------------------------------------------
 +
;;;      i = 3;
 +
;;;      if ( x != y ) {
 +
;;;          array[i] = 0;
 +
;;;      else
 +
;;;          array[i] = 1;
 +
;;;      }
 +
;;; ; -----------------------------------------------
 +
mov  dword[i], 3
 +
mov  ebx, dword[i]
 +
 +
if0: mov eax, dword[x]
 +
cmp eax, dword[y]
 +
je else0
 +
then0: mov byte[array + ebx*4], 0
 +
jmp endIf0
 +
else0: mov byte[array + ebx*4], 1
 +
endIf0:
 +
 
;;; -----------------------------------------------
 
;;; -----------------------------------------------
 
;;; Simple for loop
 
;;; Simple for loop

Latest revision as of 14:32, 21 November 2017

Examples



;;; examples.asm
;;; D. Thiebaut
;;; 
;;; A menagery of code snippets illustrating
;;; frequent constructs (for loops, etc)
;;; assemble with 231Lib.asm library
;;;
;;; nasm -f elf examples.asm
;;; nasm -f elf 231Lib.asm
;;; ld -melf_i386 -o examples examples.o 231Lib.o
;;; 
	section .data	
table	db	"ABCABCABCABCABCABCABCABCABCABCABC"
tableLen equ	$-table
	
array	dd	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0	
N	equ	($-array)/4
i	dd	0
x	dd	0
y	dd	0
	
	
	section	.text
	extern	_println, _printString, _printInt
	global	_start
_start:	

;;; -----------------------------------------------
;;; -----------------------------------------------
;;;      i = 3;
;;;      if ( x != y ) {
;;;          array[i] = 0;
;;;      else
;;;          array[i] = 1;
;;;      }
;;; ; -----------------------------------------------
	mov  	dword[i], 3
	mov  	ebx, dword[i]
	
if0:	mov	eax, dword[x]
	cmp	eax, dword[y]
	je	else0
then0:	mov	byte[array + ebx*4], 0
	jmp	endIf0
else0:	mov	byte[array + ebx*4], 1
endIf0:	

;;; -----------------------------------------------
;;; Simple for loop
;;; -----------------------------------------------
	mov	ecx, tableLen
	mov	ebx, table
for1:	add	byte[ebx],'a'-'A' 	;change char to lower
					; case
	inc	ebx			;point to next char
	loop	for1

	mov	ecx, table
	mov	edx, tableLen
	call	_printString
	call	_println
	
;;; -----------------------------------------------
;;; For-loop using ecx and loop and saving ecx,
;;; Version 1
;;; -----------------------------------------------
	section	.data
saveEcx	dd	0
	section	.text
	mov	ecx, tableLen		;get ready to loop tableLen
	mov	ebx, table		; times
	
for2:	mov	dword[saveEcx], ecx
	add	byte[ebx],1		;"increment" char to
					; get next logical char
					; in alphabet
	mov	ecx, ebx
	mov	edx, 1
	call	_printString		;print char just modified
	
	inc	ebx			;point to next char
	mov	ecx, dword[saveEcx]
	loop	for2

	call	_println

;;; -----------------------------------------------
;;; For-loop using ecx and loop and saving ecx in
;;; stack.  Version 2
;;; -----------------------------------------------
	mov     ecx, tableLen 		;get ready to loop tableLen
	mov     ebx, table    		; times
	
for3:  	push	ecx	      		;save ecx
	add     byte[ebx],1 		;"increment" char to

	mov     ecx, ebx
	mov     edx, 1
	call    _printString 		;print char just modified

	inc     ebx	     		;point to next char
	pop	ecx	     		;get ecx back from stack
	loop    for3

	call    _println

;;; -----------------------------------------------
;;; For-loop using ecx and loop and saving ecx in
;;; stack.  Runs on array of ints.  Version 3
;;; -----------------------------------------------
	mov     ecx, N 			;get ready to loop tableLen
	mov     ebx, array    		; times
	
for4:  	push	ecx	      		;save ecx
	add     dword[ebx],ecx 		;"increment" int with ecx
					; just for fun
	
	mov	eax, dword[ebx]
	call    _printInt 		;print char just modified

	add     ebx, 4	     		;point to next int (4 bytes away)
	pop	ecx	     		;get ecx back from stack
	loop    for4

	call    _println
	
;;; -----------------------------------------------
;;; For-loop using an index i
;;; 
;;; for (i = 0; i < N; i += 3 ) {
;;;     array[i] = 10;
;;; 	print( array[i] );
;;; }
;;; -----------------------------------------------
	section	.data
i	dd	0
	section	.text

	mov	dword[i], 0
for5:	cmp	dword[i], N
	jge	endFor5

	mov	ebx, dword[i]	      ;ebx <- i
	add	ebx, ebx	      ;ebx <- 2*i
	add	ebx, ebx	      ;ebx <- 4*i = offset of
				      ; array[i] in array 
	
	mov	dword[ebx+array], 10 ;array[i] <- 10

	call	_printInt

	add	dword[i], 3	      ;i += 3
	jmp	for5
endFor5:	
	call	_println
	

;;; -----------------------------------------------
;;; For-loop using an index j.  Same as previous
;;; loop, except uses option of multiplying ebx by
;;; 4 when ebx represents an index in an array of
;;; dwords.
;;; for (j = 0; j < N; j += 1 ) {
;;;     array[j] = j;
;;; 	print( array[j] );
;;; }
;;; -----------------------------------------------
	section	.data
j	dd	0
	section	.text

	mov	dword[j], 0
for6:	cmp	dword[j], N
	jge	endFor6

	mov	ebx, dword[j]	      ;ebx <- j
	mov	eax, dword[j]	      ;eax <- j
	mov	dword[ebx*4+array], eax ;array[i] <- 10
				      ;Note the "ebx*4"
				      ; part of the indirect
				      ; mode.  This is because
				      ; the array is an array of ints,
				      ; which are multiples of 4 bytes.
	mov	eax, dword[ebx*4+array]
	call	_printInt

	inc	dword[j]	      
	jmp	for6
endFor6:	
	call	_println
	
;;; -----------------------------------------------
exit:
	mov	eax, 1
	mov	ebx, 0
	int	0x80