Public feed #887052

Katie D.

public text v1 · immutable
Raw Edit
·published 2008-02-01 02:43 UTC
rendered paste body
.section .data

   message1:
     .ascii "It will get more exciting later.\n\0"

   message2:
      .ascii "But for now, let's just do some simple math.\n\0"

   message3:
      .ascii "The sum of the first 5 primes is \0"

   numbers:
     .long 2, 3, 5, 7, 11

.section .text

.globl _start

_start:

#####################
# Print our messages#
#####################

    movl $0, %edi                   # set index pointer to 0
    movl $4, %eax                   # syscall -- write()
    movl $1, %ebx                   # file descriptor '1' (STDOUT)
    movl $message1, %ecx            # put start address of string in %ecx
    movl $34,%edx                   # first string is 33 chars long
    int  $0x80                      # interrupt 80 -- Linux syscall

    movl $4, %eax                   # syscall -- write()
    movl $1, %ebx                   # file descriptor '1' (STDOUT)
    movl $message2, %ecx            # put start address of string in %ecx
    movl $47,%edx                   # second string is 47 chars long
    int  $0x80                      # interrupt 80 -- Linux syscall

    movl $4, %eax                   # syscall -- write()
    movl $1, %ebx                   # file descriptor '1' (STDOUT)
    movl $message3, %ecx            # put start address of string in %ecx
    movl $35,%edx                   # third string is 35 chars long
    int  $0x80                      # interrupt 80 -- Linux syscall

######################
# Add the numbers up #
######################

# %edi will hold our current position
# %ecx will hold the counter
# %edx will hold the current sum

    movl $0, %edi                   # start at the first byte 0
    movl $0, %edx                   # initialize sum variable to 0
    movl $5, %ecx                   # set counter to 5 (because we have 5 numbers)

add_loop:
    cmpl $0, %ecx                   # check to see if the counter is 0 yet
    je print_sum                    # if so, we're done, so print the result
                                    # otherwise, continue adding the numbers
    addl $numbers(,%edi,4), %edx    # add the next element to the sum in %ebx
    subl $1, $ecx                   # decrement the counter
    jmp add_loop                    # go back to the top of the loop

print_sum:
    movl $4, %eax                   # syscall -- write()
    movl $1, %ebx                   # file descriptor '1' (STDOUT)

    ????????????
    movl ????, %ecx                 # put start address of string in %ecx
    movl $4, %edx                   # printing a 4 byte int

################
# Exit Program #
################

    movl $1, %eax                   # syscall -- exit()
    movl $0, %ebx                   # return (0)
    int  $0x80                      # interrupt 80 -- Linux syscall