Advertisement
Guest User

CandySplittingASM

a guest
May 8th, 2011
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     # I'm not doing any retries or buffering, so it'll have to
  2.     # all come in one call or fail.  The problem statement mostly
  3.     # constrains the input data size to under 1M, so it should be
  4.     # ok with a redirect on most systems.  But interactive mode
  5.     # definitely won't work :->
  6.    
  7.     # Read input in
  8.  
  9.     mov $input, %esi
  10.     mov $1048576, %edx
  11.     xor %edi, %edi
  12.     xor %eax,%eax
  13.     syscall
  14.     mov $input, %r15
  15.  
  16.     # Read test count
  17.     call    next_int
  18.    
  19.     # Initialize accumulators
  20. test:   push    %ax
  21.     mov $1000000, %r12
  22.     mov $0, %r13
  23.     mov $0, %r14
  24.  
  25.     # Read candy count
  26.     call    next_int
  27.     mov %rax, %r11
  28.  
  29.     # Read next candy value
  30. candy:  test    %r11,%r11
  31.     jz  print
  32.     call    next_int
  33.  
  34.     # Update accumulators
  35.     cmp %r12, %rax
  36.     jge wdone
  37.     mov %rax, %r12  # keep worst's value
  38. wdone:  add %rax, %r13  # keep sum
  39.     xor %rax, %r14  # keep "almost" sum
  40.  
  41.     # More candy?
  42.     dec %r11
  43.     jmp candy
  44.  
  45.     # Display results header
  46. print:  movl    $data, %esi # "Case #"
  47.     movl    $6, %edx
  48.     movl    $1, %edi
  49.     mov $1,%eax
  50.     syscall
  51.     incw    tt(%rip)
  52.     movswl  tt(%rip), %eax  # test counter
  53.     call    convert
  54.     movl    $data+6, %esi   # ": "
  55.     movl    $2, %edx
  56.     movl    $1, %edi
  57.     mov $1,%eax
  58.     syscall
  59.  
  60.     # Check result category
  61.     cmp $0, %r14
  62.     je  yes
  63.     movl    $data+8, %esi   # "NO"
  64.     movl    $2, %edx
  65.     movl    $1, %edi
  66.     mov $1,%eax
  67.     syscall
  68.     jmp eol
  69. yes:    mov %r13, %rax  # sum - worst
  70.     sub %r12, %rax
  71.     call    convert
  72. eol:    movl    $data+10, %esi  # \n
  73.     movl    $1, %edx
  74.     movl    $1, %edi
  75.     mov $1,%eax
  76.     syscall
  77.  
  78.     # Check test counter and loop
  79.     pop %ax
  80.     cmp tt(%rip), %ax
  81.     jne test
  82.  
  83.     # And quit
  84.     mov $60,%rax
  85.     syscall
  86.  
  87. next_int:
  88.     # Skip non-decimal
  89.     mov %r15, %rdx
  90. skip:   mov %rdx, %rax
  91.     inc %rdx
  92.     mov (%rax), %cl
  93.     sub $'0', %ecx
  94.     cmp $9, %cl
  95.     ja  skip
  96.     mov $0, %rdx
  97.     jmp check
  98.     # Then convert
  99. encode: imul    $10, %rdx, %rdx
  100.     movsbq  %cl, %rcx
  101.     lea -'0'(%rdx,%rcx), %rdx
  102. check:  mov (%rax), %cl
  103.     mov %rax, %r15
  104.     inc %rax
  105.     lea -'0'(%rcx), %edi
  106.     cmp $9, %dil
  107.     jbe encode
  108.     mov %rdx, %rax
  109.     ret
  110.  
  111. convert:
  112.     mov $buf+10, %esi
  113.     mov $10, %ecx
  114.     # Check for termination
  115. cloop:  test    %eax, %eax
  116.     je  pr
  117.     # Divide
  118.     cltd
  119.     div %ecx
  120.     add $'0', %edx
  121.     dec %rsi
  122.     mov %dl, (%rsi)
  123.     jmp cloop
  124.     # Print
  125. pr: mov $buf+10, %edx
  126.     mov $1, %edi
  127.     sub %rsi, %rdx
  128.     mov $1,%eax
  129.     syscall
  130.     ret
  131.  
  132.     .data
  133. data:   .ascii "Case #: NO\n"
  134.  
  135.     .bss
  136. tt: .word   0
  137. buf:    .space  10
  138. input:  .space  1048576
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement