Running Y86 Programs

Today we will look at how to run and write Y86-64 programs.

Download and extract the y86-examples.tar.

cd cs224
wget --no-check-certificate https://cs224.cs.vassar.edu/labs/y86-examples.tar
tar -xvf y86-examples.tar
cd y86-examples

This tarfile has two programs.

We will use is to assemble and run the asum.ys program, which sums the numbers in an array.

# Execution begins at address 0 
    .pos 0
    irmovq stack, %rsp      # Set up stack pointer
    call main       # Execute main program
    halt            # Terminate program 

# Array of 4 elements
    .align 8
array:
    .quad 0x000d000d000d
    .quad 0x00c000c000c0
    .quad 0x0b000b000b00
    .quad 0xa000a000a000

main:
    irmovq array, %rdi
    irmovq $4, %rsi
    call sum             # sum(array, 4)
    ret

# long sum(long *start, long count)
# start in %rdi, count in %rsi
sum:
    irmovq $8, %r8       # Constant 8
    irmovq $1, %r9       # Constant 1
    xorq   %rax, %rax    # sum = 0
    andq   %rsi, %rsi    # Set CC
    jmp    test          # Goto test
loop:
    mrmovq (%rdi), %r10  # Get *start
    addq   %r10, %rax    # Add to sum
    addq   %r8, %rdi     # start++
    subq   %r9, %rsi     # count--.  Set CC
test:
    jne    loop          # Stop when 0
    ret                  # Return

# Stack starts here and grows to lower addresses
    .pos 0x200
stack:
                         # Must end with a blank line

To assemble the program:

./yas asum.ys

This will create the file asum.yo. This is a Y86-64 object code file, which is also a text file. Let’s have a look.

                            | # Execution begins at address 0 
0x000:                      |     .pos 0
0x000: 30f40002000000000000 |     irmovq stack, %rsp      # Set up stack pointer
0x00a: 803800000000000000   |     call main       # Execute main program
0x013: 00                   |     halt            # Terminate program 
                            | 
                            | # Array of 4 elements
0x018:                      |     .align 8
0x018:                      | array:
0x018: 0d000d000d000000     |     .quad 0x000d000d000d
0x020: c000c000c0000000     |     .quad 0x00c000c000c0
0x028: 000b000b000b0000     |     .quad 0x0b000b000b00
0x030: 00a000a000a00000     |     .quad 0xa000a000a000
                            | 
0x038:                      | main:
0x038: 30f71800000000000000 |     irmovq array, %rdi
0x042: 30f60400000000000000 |     irmovq $4, %rsi
0x04c: 805600000000000000   |     call sum             # sum(array, 4)
0x055: 90                   |     ret
                            | 
                            | # long sum(long *start, long count)
                            | # start in %rdi, count in %rsi
0x056:                      | sum:
0x056: 30f80800000000000000 |     irmovq $8, %r8       # Constant 8
0x060: 30f90100000000000000 |     irmovq $1, %r9       # Constant 1
0x06a: 6300                 |     xorq   %rax, %rax    # sum = 0
0x06c: 6266                 |     andq   %rsi, %rsi    # Set CC
0x06e: 708700000000000000   |     jmp    test          # Goto test
0x077:                      | loop:
0x077: 50a70000000000000000 |     mrmovq (%rdi), %r10  # Get *start
0x081: 60a0                 |     addq   %r10, %rax    # Add to sum
0x083: 6087                 |     addq   %r8, %rdi     # start++
0x085: 6196                 |     subq   %r9, %rsi     # count--.  Set CC
0x087:                      | test:
0x087: 747700000000000000   |     jne    loop          # Stop when 0
0x090: 90                   |     ret                  # Return
                            | 
                            | # Stack starts here and grows to lower addresses
0x200:                      |     .pos 0x200
0x200:                      | stack:
                            |                          # Must end with a blank line

To run our object file, we can use the use the yis program.

./yis asum.yo

You output should look like this:

Stopped in 34 steps at PC = 0x13.  Status 'HLT', CC Z=1 S=0 O=0
Changes to registers:
%rax:	0x0000000000000000	0x0000abcdabcdabcd
%rsp:	0x0000000000000000	0x0000000000000200
%rdi:	0x0000000000000000	0x0000000000000038
%r8:	0x0000000000000000	0x0000000000000008
%r9:	0x0000000000000000	0x0000000000000001
%r10:	0x0000000000000000	0x0000a000a000a000

Changes to memory:
0x01f0:	0x0000000000000000	0x0000000000000055
0x01f8:	0x0000000000000000	0x0000000000000013

Your lab assignment is to finish the implementation of asumr.ys, which sums an array of numbers recursively. When you are finished, submit asumr.ys to gradescope.