Advertisement
Guest User

Untitled

a guest
May 10th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #comment  
  2. .section .bss
  3.  
  4. .section .data
  5. fstring:
  6.   .asciz "the result is %d\n"
  7.  
  8. ##INSERT YOUR CODE HERE
  9.  
  10. iarr:
  11. .int 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  12.  
  13. ##INSERT YOUR CODE HERE END
  14.  
  15. .section .text
  16.  
  17. ##INSERT YOUR CODE HERE
  18. #func func
  19. #descr: Sum via pointer in rax
  20. #receives: RDI Pointer = int  
  21. #returns: RAX=int
  22.  
  23.     #fn entry (save old rbp and init new rbp with rsp)
  24.     ##allocate memory on stack(local vars)
  25.     #subq <numbytes>, %rsp    
  26.     #actual code
  27. func:
  28. jmp WHILE
  29.  
  30. LOOP:
  31. subq $1, %r8 #schleifendurchgänge
  32. movq (%rdi,%r9,4),%rdx #element + itterator in rdx
  33. addq %rdx, %rax #ergebnis = ergebnis + element im array
  34. addq $1, %r9 #itterator++
  35.  
  36. WHILE:
  37. cmp $0, %r8
  38. jne LOOP
  39.  
  40.     #fn exit (free stack (local vars), restore rpb)
  41. ret
  42. ##INSERT YOUR CODE HERE END
  43.  
  44.  
  45. .globl main #start if using gcc directly
  46. main: #start if using gcc directly
  47.  
  48.   ##INSERT YOUR CODE HERE
  49. movq $0, %rax #ergebnis return (eax)
  50. movq $0, %rcx #element im array
  51. movq $0, %r9 #itterator
  52. movq $10, %r8 #while schleife zähler für 10 elemente im array
  53. leaq iarr (,%rcx,4), %rdi #pointer auf das array
  54.     #call of func
  55. call func
  56.     ##hand over parameters via reg (order RDI, RSI, RDX, RCX, R8, R9; XMM0–7)
  57.    
  58.     ##call
  59.    
  60.     ##free stack (allocated paramters)
  61.     #addq <numbytes>, %rsp
  62.   ##INSERT YOUR CODE HERE END
  63.  
  64.     #call of printf
  65.     ##hand over parameters via reg (order RDI, RSI, RDX, RCX, R8, R9; XMM0–7)
  66.   movq %rax, %rsi
  67.   leaq fstring, %rax
  68.   movq %rax, %rdi
  69.   #movq $fstring, %rdi
  70.   movq $0, %rax #%rax holds how many xmm regs to be used in x86-64
  71.     ##call
  72.     call printf
  73.     ##free stack (allocated paramters)
  74.     #addq <numbytes>, %rsp
  75.  
  76.   #program exit via ret (if using gcc directly)
  77.   movq $0, %rax # return 0 (success)
  78.   ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement