Created
October 25, 2018 08:39
-
-
Save faebser/287ea3885438f3100ceeb699957d7129 to your computer and use it in GitHub Desktop.
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
# Fabian Frei | |
# | |
# PURPOSE: This program calculates the remainder of the product of a | |
# set of integer data items (long values). | |
# | |
# VARIABLES: The registers have the following uses: | |
# | |
# %rcx - Holds the index of the data item being examined, changed from %rdx | |
# %rbx - current data item | |
# %rax - product of current multiplication in loop | |
# %rdi - for the exit code of our program | |
# | |
# The following memory locations are used: | |
# | |
# data_items - contains the item data. A 0 at the end data_items is used | |
# to terminate the data. Only positive values are allowed. | |
# | |
.section .data | |
data_items: # These are the data items | |
.quad 2,4,3,7,3,4,0 | |
#.quad 0 # for testing of zero length data items | |
.section .text | |
.globl _start | |
_start: | |
movq $0, %rcx # move 0 into the index register | |
movq data_items(,%rcx,8), %rax # load the first byte of data to %rax for multiplication | |
cmpq $0, %rax # check %rax if first value is zero | |
je early_loop_exit # if null we exit early and never enter the loop | |
start_loop: # start loop | |
incq %rcx # increment data items counter | |
movq data_items(,%rcx,8), %rbx # move next data item into rbx | |
cmpq $0, %rbx # check to see if we've hit the end | |
je loop_exit # if yes we go to regular loop exit | |
imulq %rbx # multiply current item with %rax and save it to %rax | |
jmp start_loop # jump to loop beginning | |
loop_exit: | |
#movq $128, %r8 # move 128 into %r8 for division. | |
idiv $128 # divide %rax by 128, stores quotient in %rax, remainder in %rdx | |
movq %rdx, %rdi # move remainder from %rdx into %rdi as exit code | |
movq $60, %rax # 60 is the exit() syscall | |
syscall | |
early_loop_exit: | |
movq $1, %rdi # move 1 as exit code into %rdi | |
movq $60, %rax # 60 is the exit() syscall | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment