944,041 Members | Top Members by Rank

Ad:
0

Bin2Time: Unsigned milliseconds to time format hhh:mm:ss.sss

by on Feb 1st, 2008
I use GetTickCount in a lot of my applications, so I built this snippet to convert to hours minutes and seconds. Leading extraneous data is excluded from output.
Assembly Code Snippet (Toggle Plain Text)
  1. Entry ECX = Unsigned value of duration in milliseconds
  2. EDX = Pointer to ASCII output.
  3.  
  4. Divisors dd 3600000, 60000, 1
  5.  
  6. HrsFmt db '%d:', 0
  7. MinFmt db '%02d:', 0
  8. SecFmt db '%06d', 0
  9. Formats dd HrsFmt, MinFmt, SecFmt
  10.  
  11. 0 57 push edi
  12. 1 56 push esi
  13. 2 53 push ebx
  14.  
  15. ; Epilog set up for applications loop
  16.  
  17. 3 52 push edx ; Preserve initial value
  18. 4 BE <-> mov esi, Divisors
  19. 9 8BFA mov edi, edx
  20. B BB <-> mov ebx, Formats
  21. 10 8BD1 mov edx, ecx
  22. 12 33C9 xor ecx, ecx
  23. 14 880F mov [edi], cl ; Nullify previous contents
  24. 16 B1 03 mov cl, 3
  25. 18 51 push ecx
  26.  
  27. 19 AD lodsd ; Get next divisor
  28. 1A 8BC8 mov ecx, eax
  29. 1C 8BC2 mov eax, edx
  30. 1E 33D2 xor edx, edx
  31. 20 F7F9 idiv ecx
  32. 22 8BC8 mov ecx, eax
  33. 24 87F3 xchg ebx, esi
  34. 26 AD lodsd ; Get pointer to next format string
  35. 27 87F3 xchg ebx, esi
  36. 29 23C9 and ecx, ecx
  37. 2B 75 05 jnz 32 ; Was quotient null
  38. 2D 803F 00 cmp byte ptr [edi], 0 ; Has anything been written to output
  39. 30 74 0F je 59
  40.  
  41. Write next segment of string with applicable format
  42.  
  43. 32 52 push edx
  44. 33 51 push ecx
  45. 34 50 push eax
  46. 35 57 push edi
  47. 36 E8 <-> call wsprintf ; Make this segment of string
  48. 3B 83C4 0C add esp, 0C
  49. 3E 5A pop edx
  50. 3F 03F8 add edi, eax
  51. 41 59 pop ecx
  52. 42 E2 D4 loopd 19
  53.  
  54. The last part of output always has 6 characters and positions 1 & 2 must be moved to 0 & 1 and 2
  55. replaced with the decimal point
  56.  
  57. 44 8BCF mov ecx, edi
  58. 46 2BF8 sub edi, eax
  59. 48 66:8B47 01 mov ax, [edi+1]
  60. 4C 66:AB stosw
  61. 4E C607 2E mov byte ptr [edi], 2E
  62. 51 5F pop edi
  63. 52 2BCF sub ecx, edi
  64. 54 803F 30 cmp byte ptr [edi], 30
  65. 57 75 0A jnz 63
  66.  
  67. The possibility that either hours or seconds can have a leading zero and they must
  68. be removed from output by shifting everything upward by 1
  69.  
  70. 59 8BF7 mov esi, edi
  71. 5B 51 push ecx
  72. 5C 57 push edi
  73. 5D 46 inc esi
  74. 5E F3:A4 rep movsb
  75. 60 5F pop edi
  76. 61 59 pop ecx
  77. 62 49 dec ecx
  78.  
  79. Epilog ECX = Number of characters in output
  80. EDX = Unchanged, pointer to ASCII string
  81.  
  82. 63 8BD7 mov edx, edi
  83. 65 33C0 xor eax, eax
  84. 67 5B pop ebx
  85. 68 5E pop esi
  86. 69 5F pop edi
  87. 6A C3 ret
  88.  
  89. 6B = 107 bytes
Message:
Previous Thread in Assembly Forum Timeline: Need help with this program please
Next Thread in Assembly Forum Timeline: ARM Assembly . Keypress





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC