-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b30752
commit 09970e0
Showing
3 changed files
with
1,022 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
TITLE MASM Template (homework5.asm) | ||
|
||
; Carlton Semple | ||
; BubbleSort Lab | ||
; Revision date: | ||
|
||
|
||
INCLUDE Irvine32.inc | ||
.data | ||
makecount = 10000 | ||
count DWORD ? | ||
innercount = 9999 | ||
;array DWORD makecount DUP(?) | ||
array DWORD makecount DUP(?) | ||
|
||
startTime DWORD ? | ||
message1 BYTE "Elapsed Time in Milliseconds: ", 0 | ||
|
||
.code | ||
main PROC | ||
|
||
mov esi, 0 ; used to iterate through the array | ||
mov ecx, makecount ; ecx contains the number of times the loop should run | ||
;mov ebx, 100 ; use ebx for a test | ||
call Randomize | ||
GenerateRandomArray: | ||
mov eax, 10000 | ||
call RandomRange | ||
mov array[esi * TYPE array], eax ; switch to ebx for testing 1 - 10 | ||
;;;;;; mov ebx, array[esi * TYPE array] ; test to see that the array values are correct | ||
inc esi | ||
dec ebx | ||
loop GenerateRandomArray | ||
|
||
; timer | ||
call GetMSeconds | ||
mov startTime, eax | ||
|
||
call bubbleSort | ||
call printArray | ||
|
||
call GetMSeconds | ||
sub eax, startTime | ||
|
||
mov edx, OFFSET message1 | ||
call WriteString | ||
call WriteDec | ||
call Crlf | ||
|
||
exit | ||
main ENDP | ||
|
||
|
||
bubbleSort PROC | ||
mov ecx, makecount ; initialize ecx | ||
|
||
OuterLoop: | ||
mov count, ecx ; save outer loop count (page 126 in book) | ||
|
||
; the following is for the inner loop | ||
mov ecx, innercount ; count - 1, the count for the inner loop | ||
mov esi, 0 ; esi is the index | ||
mov edi, 1 ; edi is the ahead index: esi + 1 | ||
|
||
InnerLoop: | ||
mov ebx, array[esi * TYPE array] | ||
mov edx, array[edi * TYPE array] | ||
cmp ebx, edx ; compare array[i] & array[i+1] | ||
JLE next ; do nothing if array[i] <= array[i+1] | ||
; else if (array[i] > array[i+1]) | ||
|
||
; swap | ||
mov ebx, array[esi * TYPE array] ; ebx = array[i] | ||
xchg ebx, array[edi * TYPE array] ; ebx = array[i+1]. array[i+1] = array[i] | ||
xchg ebx, array[esi * TYPE array] ; ebx = array[i]. array[i] = array[i+1] | ||
|
||
next: | ||
inc esi | ||
inc edi | ||
loop InnerLoop ; end of the inner loop | ||
mov ecx, count ; restore the value of ecx for the outer loop | ||
loop OuterLoop ; ecx = ecx - 1 | ||
|
||
ret | ||
bubbleSort ENDP | ||
|
||
printArray PROC | ||
|
||
mov ecx, makecount | ||
mov esi, 0 | ||
print: | ||
mov eax, array[esi * TYPE array] | ||
call WriteDec | ||
call Crlf ; newline | ||
inc esi | ||
loop print | ||
|
||
ret | ||
printArray ENDP | ||
|
||
|
||
END main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,278 @@ | ||
TITLE MASM Template (SempleCarlton_Calculator.asm) | ||
|
||
; Carlton Semple | ||
; Calculator Lab | ||
; Date: 4/27/2014 | ||
|
||
|
||
INCLUDE Irvine32.inc | ||
.data | ||
welcome BYTE "---------- Carlton's Calculator ----------", 0 | ||
intro1 BYTE "1 = Addition", 0 | ||
intro2 BYTE "2 = Subtraction", 0 | ||
intro3 BYTE "3 = Multiplication", 0 | ||
intro4 BYTE "4 = Division", 0 | ||
intro5 BYTE "5 = Quit", 0 | ||
correctiveStatement BYTE "Please enter a valid choice", 0 | ||
farewell BYTE "Goodbye", 0 | ||
|
||
prompt1 BYTE "Enter the first number: ", 0 | ||
prompt2 BYTE "Enter the second number: ", 0 | ||
success BYTE "Result: ", 0 | ||
|
||
; symbols to make things look nicer | ||
plus BYTE " + ", 0 | ||
minus BYTE " - ", 0 | ||
star BYTE " * ", 0 | ||
slash BYTE " / ", 0 | ||
equalsign BYTE " = ", 0 | ||
|
||
; zero error | ||
NoZeroDenominator BYTE "The denominator cannot be 0. Reenter the divisor", 0 | ||
choice SDWORD ? | ||
number1 SDWORD ? | ||
number2 SDWORD ? | ||
result SDWORD ? | ||
.code | ||
main PROC | ||
Begin: | ||
call menu | ||
|
||
cmp choice, 1 | ||
jl Retry | ||
cmp choice, 5 | ||
jg Retry | ||
cmp choice, 5 | ||
je Quit ; quit if 5 is the choice | ||
|
||
;; decide what to do based on the value of choice | ||
call decide | ||
jmp Begin | ||
|
||
Retry: | ||
;; remind the user to enter a valid choice | ||
mov edx, OFFSET correctiveStatement | ||
call WriteSTring | ||
call crlf | ||
call crlf | ||
jmp Begin ; jump back to the beginning if none of the options were chosen | ||
|
||
Quit: | ||
mov edx, OFFSET farewell | ||
call WriteString | ||
exit | ||
main ENDP | ||
|
||
menu PROC | ||
;; print out the options using edx and WriteString | ||
|
||
mov edx, OFFSET welcome ; welcome message | ||
call WriteString | ||
call crlf | ||
mov edx, OFFSET intro1 ; option 1 | ||
call WriteString | ||
call crlf | ||
mov edx, OFFSET intro2 ; option 2 | ||
call WriteString | ||
call crlf | ||
mov edx, OFFSET intro3 ; option 3 | ||
call WriteString | ||
call crlf | ||
mov edx, OFFSET intro4 ; option 4 | ||
call WriteSTring | ||
call crlf | ||
mov edx, OFFSET intro5 ; option to quit | ||
call WriteString | ||
call crlf | ||
|
||
;; move the input into choice, which will be used by another function to determine what to do | ||
|
||
call ReadInt | ||
mov choice, eax | ||
ret | ||
menu ENDP | ||
|
||
decide PROC | ||
; Get the numbers first | ||
|
||
;; get the first number | ||
mov edx, OFFSET prompt1 | ||
call WriteString | ||
call crlf | ||
call ReadInt ;; prepare for signed numbers | ||
mov number1, eax | ||
|
||
;; get the second number | ||
mov edx, OFFSET prompt2 | ||
call WriteString | ||
call crlf | ||
call ReadInt | ||
mov number2, eax | ||
|
||
; Decide what to do based on the value of choice | ||
|
||
; addition | ||
cmp choice, 1 | ||
je addition | ||
|
||
; subtraction | ||
cmp choice, 2 | ||
je subtraction | ||
; multiplication | ||
cmp choice, 3 | ||
je multiplication | ||
; division | ||
cmp choice, 4 | ||
je division | ||
|
||
ret | ||
decide ENDP | ||
|
||
addition PROC | ||
|
||
; add the numbers | ||
mov eax, number1 | ||
add eax, number2 | ||
mov result, eax ; save the result | ||
|
||
; display result | ||
mov eax, number1 | ||
call crlf | ||
call WriteInt ; display first number | ||
|
||
mov edx, OFFSET plus | ||
call WriteString ; display " + " | ||
|
||
mov eax, number2 | ||
call WriteInt ; display second number | ||
|
||
mov edx, OFFSET equalsign | ||
call WriteString ; display " = " | ||
mov eax, result | ||
call WriteInt ; display result | ||
|
||
call crlf | ||
call crlf | ||
ret | ||
addition ENDP | ||
|
||
subtraction PROC | ||
; subtract the numbers | ||
mov eax, number1 | ||
sub eax, number2 | ||
mov result, eax ; save the result | ||
|
||
; display result | ||
mov eax, number1 | ||
call crlf | ||
call WriteInt ; display first number | ||
|
||
mov edx, OFFSET minus | ||
call WriteString ; display " + " | ||
|
||
mov eax, number2 | ||
call WriteInt ; display second number | ||
|
||
mov edx, OFFSET equalsign | ||
call WriteString ; display " = " | ||
mov eax, result | ||
call WriteInt ; display result | ||
|
||
call crlf | ||
call crlf | ||
|
||
ret | ||
subtraction ENDP | ||
|
||
multiplication PROC | ||
; multiply the numbers | ||
mov eax, number1 | ||
mov ebx, number2 ; move the number to a register first | ||
imul ebx ; multiply eax by ebx | ||
mov result, eax ; save the result | ||
|
||
; display result | ||
mov eax, number1 | ||
call crlf | ||
call WriteInt ; display first number | ||
|
||
mov edx, OFFSET star | ||
call WriteString ; display " + " | ||
|
||
mov eax, number2 | ||
call WriteInt ; display second number | ||
|
||
mov edx, OFFSET equalsign | ||
call WriteString ; display " = " | ||
mov eax, result | ||
call WriteInt ; display result | ||
|
||
call crlf | ||
call crlf | ||
|
||
ret | ||
multiplication ENDP | ||
|
||
division PROC | ||
; divide the numbers | ||
mov eax, number1 | ||
cdq | ||
mov ebx, number2 ; move the number to a register first | ||
|
||
; make sure the divisor isn't 0 | ||
Check: | ||
cmp ebx, 0 | ||
jz NoZeroesAllowed ; tell the user that 0 isn't allowed as the denominator | ||
|
||
idiv ebx ; divide eax by ebx | ||
mov result, eax ; save the result | ||
|
||
; display result | ||
mov eax, number1 | ||
call crlf | ||
call WriteInt ; display first number | ||
|
||
mov edx, OFFSET star | ||
call WriteString ; display " + " | ||
|
||
mov eax, number2 | ||
call WriteInt ; display second number | ||
|
||
mov edx, OFFSET equalsign | ||
call WriteString ; display " = " | ||
mov eax, result | ||
call WriteInt ; display result | ||
|
||
call crlf | ||
call crlf | ||
jmp EndDivision | ||
|
||
NoZeroesAllowed: ; tell the user that 0 isn't allowed as the denominator, then retry | ||
mov edx, OFFSET NoZeroDenominator | ||
call WriteString | ||
call crlf | ||
call ReadInt | ||
mov number2, eax | ||
jmp check | ||
|
||
EndDivision: | ||
ret | ||
division ENDP | ||
|
||
|
||
END main |
Oops, something went wrong.