flat assembler
Message board for the users of flat assembler.

Index > Windows > Math logic

Author
Thread Post new topic Reply to topic
luke77



Joined: 14 May 2010
Posts: 18
luke77 06 Jun 2010, 01:41
I am trying to come up with the logic to convert milliseconds to
seconds,minutes, and hours.

This is what I have.
Alright, let me have it. Very Happy

; seconds = milliseconds/1000
; minutes = seconds/60
; hours = minutes/60
; days = hours/24

Ex. 1,514,047 milliseconds

Divide result by 1000,

if less than 1, answer is in milliseconds
if greater than 1 or less than 60, answer is in seconds
if 60 or less than 3600, answer is in minutes
if 3600 or less than 216000, answer is in hours
Post 06 Jun 2010, 01:41
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 06 Jun 2010, 03:02
Yeah, that makes sense, what about it? But IMO, dividing in asm is a pain. I'm too lazy to use asm for anything that needs dividing Smile.
Post 06 Jun 2010, 03:02
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 06 Jun 2010, 03:32
Note sure what you're actually want so I've decided to guess and write this program Razz
Code:
format pe console
include 'win32ax.inc'

      mov     ebx, 1

.loop:
      mov     eax, ebx

      mov     esi, 0
      mov     ecx, 1000
      xor     edx, edx
      div     ecx
      push    edx

      test    eax, eax
      jz      .print

      inc     esi
      mov     ecx, 60
      xor     edx, edx
      div     ecx
      push    edx

      test    eax, eax
      jz      .print

      inc     esi
      xor     edx, edx
      div     ecx ; ECX IS STILL 60 HERE
      push    edx

      test    eax, eax
      jz      .print

      inc     esi
      mov     ecx, 24
      xor     edx, edx
      div     ecx
      push    edx

      test    eax, eax
      jz      .print

      inc     esi
      push    eax

.print:
      push    [strTab+esi*4]
      call    [printf]
      lea     esp, [esp+esi*4+8]

      add     ebx, ebx
      jnc     .loop

      cinvoke system, cmd
      invoke  ExitProcess, 0

strTab dd fmtMs, fmtSeg, fmtMin, fmtHs, fmtDays

fmtMs   db "%u millisecond(s).", 13, 10, 0
fmtSeg  db "%u second(s) and %u millisecond(s).", 13, 10, 0
fmtMin  db "%u minute(s), %u second(s) and %u millisecond(s).", 13, 10, 0
fmtHs   db "%u hour(s), %u minute(s), %u second(s) and %u millisecond(s).", 13, 10, 0
fmtDays db "%u day(s), %u hour(s), %u minute(s), %u second(s) and %u millisecond(s).", 13, 10, 0

cmd db "pause", 10, 0

align 4 ; Just to be safe
data import 
  library kernel32, 'kernel32.dll',\
          msvcrt,'msvcrt.dll'

  include 'api/kernel32.inc'

  import msvcrt,\
         printf, 'printf',\
         system, 'system'
end data    
Output on my PC:
Code:
1 millisecond(s).
2 millisecond(s).
4 millisecond(s).
8 millisecond(s).
16 millisecond(s).
32 millisecond(s).
64 millisecond(s).
128 millisecond(s).
256 millisecond(s).
512 millisecond(s).
1 second(s) and 24 millisecond(s).
2 second(s) and 48 millisecond(s).
4 second(s) and 96 millisecond(s).
8 second(s) and 192 millisecond(s).
16 second(s) and 384 millisecond(s).
32 second(s) and 768 millisecond(s).
1 minute(s), 5 second(s) and 536 millisecond(s).
2 minute(s), 11 second(s) and 72 millisecond(s).
4 minute(s), 22 second(s) and 144 millisecond(s).
8 minute(s), 44 second(s) and 288 millisecond(s).
17 minute(s), 28 second(s) and 576 millisecond(s).
34 minute(s), 57 second(s) and 152 millisecond(s).
1 hour(s), 9 minute(s), 54 second(s) and 304 millisecond(s).
2 hour(s), 19 minute(s), 48 second(s) and 608 millisecond(s).
4 hour(s), 39 minute(s), 37 second(s) and 216 millisecond(s).
9 hour(s), 19 minute(s), 14 second(s) and 432 millisecond(s).
18 hour(s), 38 minute(s), 28 second(s) and 864 millisecond(s).
1 day(s), 13 hour(s), 16 minute(s), 57 second(s) and 728 millisecond(s).
3 day(s), 2 hour(s), 33 minute(s), 55 second(s) and 456 millisecond(s).
6 day(s), 5 hour(s), 7 minute(s), 50 second(s) and 912 millisecond(s).
12 day(s), 10 hour(s), 15 minute(s), 41 second(s) and 824 millisecond(s).
24 day(s), 20 hour(s), 31 minute(s), 23 second(s) and 648 millisecond(s).
Presione una tecla para continuar . . .    


PS: The code was meant to be straightforward, not size nor speed optimization was attempted.
Post 06 Jun 2010, 03:32
View user's profile Send private message Reply with quote
FrozenKnight



Joined: 24 Jun 2005
Posts: 128
FrozenKnight 01 Jul 2010, 10:33
What you could do is use multiplication and take advantage of overflow.
Post 01 Jul 2010, 10:33
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.