fork download
  1. .intel_syntax noprefix
  2. .global main
  3. main:
  4. sub esp, 12
  5. mov [esp + 8], ebx
  6. xor ebx, ebx
  7.  
  8. test_loop:
  9. mov eax, [in + 4 * ebx]
  10. mov dword ptr [esp], eax
  11. call bitstuffing
  12. mov [esp + 8], eax
  13. cmp eax, [verify + 4 * ebx]
  14. mov dword ptr [esp], offset ok
  15. je got_fmt
  16. mov dword ptr [esp], offset error
  17. got_fmt:
  18. mov eax, [in + 4 * ebx]
  19. mov [esp + 4], eax
  20. call printf
  21. inc ebx
  22. cmp ebx, 7
  23. jb test_loop
  24.  
  25. mov ebx, [esp + 8]
  26. add esp, 12
  27. xor eax, eax
  28. ret
  29.  
  30. bitstuffing:
  31. push ebp
  32. mov ebp, esp
  33. push ebx
  34.  
  35. mov cl, 32 # 32 bits to go
  36. xor eax, eax # the output
  37. mov edx, [ebp + 8] # the input
  38. xor bl, bl # the run count
  39. next_bit:
  40. dec cl # more bits?
  41. js done # no
  42. shl edx, 1 # consume from the input into CF
  43. rcl eax, 1 # copy to output from CF
  44. test bl, bl # first bit always matches
  45. jz match
  46. test al, 3 # do we have 00 or 11 in the low 2 bits?
  47. jnp reset # no, start counting again
  48. match:
  49. inc bl
  50. cmp bl, 5 # did 5 bits match?
  51. jb next_bit # no, keep going
  52. dec cl # space for stuffed bit?
  53. js done # no
  54. mov ebx, eax # make a copy
  55. and ebx, 1 # isolate LSB
  56. xor ebx, 1 # flip it
  57. shl eax, 1 # make space for it
  58. or eax, ebx # stuff it
  59. reset:
  60. mov bl, 1 # already have length 1
  61. jmp next_bit
  62.  
  63. done:
  64. pop ebx
  65. mov esp, ebp
  66. pop ebp
  67. ret
  68.  
  69. .data
  70. ok: .string "OK: 0x%08x => 0x%08x\n"
  71. error: .string "ERROR: 0x%08x => 0x%08x\n"
  72. in: .int 0xFFFFFFFF, 0x00000000, 0x0F0F0F0F, 0x0F0F0F00, 0x0F0F0000, 0xAAAA0000, 0x07878000
  73. verify: .int 0xFBEFBEFB, 0x04104104, 0x0F0F0F0F, 0x0F0F0F04, 0x0F0F0410, 0xAAAA0820, 0x07C1F041
Success #stdin #stdout 0s 1728KB
stdin
Standard input is empty
stdout
OK: 0xffffffff => 0xfbefbefb
OK: 0x00000000 => 0x04104104
OK: 0x0f0f0f0f => 0x0f0f0f0f
OK: 0x0f0f0f00 => 0x0f0f0f04
OK: 0x0f0f0000 => 0x0f0f0410
OK: 0xaaaa0000 => 0xaaaa0820
OK: 0x07878000 => 0x07c1f041