System: Process Pratice


add8:

    # YOUR CODE HERE
        movq %rdi, %rax
        addq %rsi, %rax
        addq %rdx, %rax
        addq %rcx, %rax
        addq %r8, %rax
        addq %r9, %rax
    
        movq 8(%rsp), %rdi
        addq %rdi, %rax
        movq 16(%rsp), %rdi
        addq %rdi, %rax
        ret    
        # END YOUR CODE
    

call_max2:

    # YOUR CODE HERE
    pushq     $10
    pushq    $20
    # 20 is at (%rsp)
    # 10 is now at 8(%rsp)
        movq    %rsp, %rsi    # Address of 20
        leaq    8(%rsp), %rdi    # Address of 10
        call    max2
        addq    $16, %rsp    # Could also do popq twice!
        ret
    # END YOUR CODE

overwrite:

    # YOUR CODE HERE
    # This is after Exercise #3 - Part 2/2
  pushq %rbx    # Callee save register, so back it up first.
    movq $100, %r11    # Overwrite R11 with 100
    movq $200, %rbx    # Overwrite RBX with 200
    popq %rbx    # Restore original RBX value back into RBX
    ret
    # END YOUR CODE

 

call_overwrite:

    # YOUR CODE HERE
    # Don't forget to also back up RBX here too!  It's callee-save!
    pushq %rbx    # Backup RBX on the Stack

    movq $10, %r11
    movq $20, %rbx

  pushq %r11    # R11 is Caller-Save, need to back it up before call
    call overwrite
    popq %r11    # Now we can put R11's value back!

    leaq (%r11, %rbx), %rax    # Add v1 + v2 into RAX

    popq %rbx    # Restore RBX
    ret
    # END YOUR CODE

rec_fact:

    # YOUR CODE HERE
    cmpq    $1, %rdi
    je    .L3
    pushq    %rbx
    movq    %rdi, %rbx
    leaq    -1(%rdi), %rdi
    # That was a fun way to decq %rdi wasn't it? :)
    call    rec_fact
    imulq    %rbx, %rax
    popq    %rbx
    ret

    

    .L3:
    movq %rdi, %rax
    ret

    # END YOUR CODE

 
原文地址:https://www.cnblogs.com/JasperZhao/p/14136029.html