CSC231 FindLargestOfArray.asm
From DftWiki
--D. Thiebaut 17:17, 1 December 2010 (UTC)
;;; findLargestOfArray.asm
;;; D. Thiebaut
;;; Finds the largest item in an array of N elements
;;;
;;; compile and link as follows:
;;;
;;; nasm -f elf -F stabs findLargestOfArray.asm
;;; gcc -o findLargestOfArray driver.c asm_io.o findLargestOfArray.o
;;;
%include "asm_io.inc"
%assign EXIT 1
;; -------------------------
;; data segment
;; -------------------------
section .data
msg1 db "large of array = ",0
array dd 1, 3, 10, 0, -2, 44, 10
N equ ($-array)/4
;; -------------------------
;; code area
;; -------------------------
section .text
global asm_main
;;; ---------------------------------------------------------
;;; main program
;;; ---------------------------------------------------------
asm_main:
;;; largest = MOSTNEG;
;;; for ( i=0; i<N; i++ ) {
;;; if ( array[i]>largest )
;;; largest = array[i];
;;;
;;; print "largest of array = ", largest
mov esi, 0
mov ebx, 0x80000000 ; most negative dword
for: cmp esi, N*4
jge done
cmp ebx, dword[array+esi]
jg next
mov ebx, dword[array+esi]
next: add esi, 4
jmp for
done:
mov eax, ebx
call printMax
ret
;;; ----------------------------------------------------------
;;; printMax: gets integer in eax and prints it
;;; ----------------------------------------------------------
printMax:
push eax ; save max in stack
mov eax, msg1
call print_string ; prints " "
pop eax ; get max from stack
call print_int ; print fact(i)
call print_nl ; next line
ret
