Skip to content

Instantly share code, notes, and snippets.

@theKidOfArcrania
Last active September 26, 2018 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theKidOfArcrania/1b26954722324ba9f31e7427eaf3144f to your computer and use it in GitHub Desktop.
Save theKidOfArcrania/1b26954722324ba9f31e7427eaf3144f to your computer and use it in GitHub Desktop.
%include "nasmx.inc"
ENTRY main
IMPORT fgets
IMPORT printf
IMPORT puts
IMPORT atoi
EXTERN stdin
section .data
input: db "arr[%d] = ", 0
print_max: db "The max number in the list is: %d", 0xa, 0
print_min: db "The min number in the list is: %d", 0xa, 0
print_mean: db "The mean is: %d", 0xa, 0
nullstr: db 0
arr: times 16 dd 0
section .text
; Computes the largest element in arr
PROC max
LOCALS NONE
; TODO: code for this one.
mov eax, 0
ENDPROC
; Computes the smallest element in arr
PROC min
LOCALS NONE
; TODO: code for this one.
mov eax, 0
ENDPROC
; Computes the mean of all the elements
PROC mean
LOCALS NONE
; TODO: code for this one.
mov eax, 0
ENDPROC
; *********************************************************
; Below this line we have some helper functions You do not need to worry about
; any of these functions.
; *********************************************************
PROC read_num, ptrdiff_t prompt, uint32_t arg
LOCALS
LOCAL buff, uint8_t, 80
ENDLOCALS
INVOKE printf, [argv(.prompt)], [var(.arg)]
lea rax, [var(.buff)]
mov rdx, [stdin]
INVOKE fgets, rax, 80, rdx
lea rax, [var(.buff)]
INVOKE atoi, rax
ENDPROC
PROC main
LOCALS
LOCAL ind, uint32_t
ENDLOCALS
; Read 16 integers from user
mov dword [var(.ind)], 0
.readloop:
INVOKE read_num, input, [var(.ind)]
mov ecx, [var(.ind)]
mov [ecx * 4 + arr], eax
add dword [var(.ind)], 1
cmp dword [var(.ind)], 16
jl .readloop ; while ind < 16
; Print out the max, min, and mean
INVOKE max
INVOKE printf, print_max, rax
INVOKE min
INVOKE printf, print_min, rax
INVOKE mean
INVOKE printf, print_mean, rax
ENDPROC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment