0

I am trying to ask a user to enter a time value in this order hours:min - for example user may enter 3:45. Now , I want to get the hours value and do something with it, then I want to take the minutes value and do something else with it.

I am using mov %o0, %time to store the time value. However, there are 2 problems:

  • the program will not run if the user enters : after the hours.
  • I do not know how to get the value of hour and time separately.

any help will be greatly appreciated.

Andy M
  • 167
  • 1
  • 2
  • 7

1 Answers1

0

Use whatever services you have for accepting string input, then find the separator character and parse the two numbers on each side.


Update: here is a sample implementation.

! parse time in hh:mm format
! in: %o0 pointer to zero-terminated buffer
! out: %o0 hours, %o1 minutes
gettime:
    save %sp, -64, %sp
    ! look for the colon in the string
    ! for simplicity, assume it always exists
    mov %i0, %o0            ! use %o0 for pointer
colon_loop:
    ldub [%o0], %o1         ! load next byte
    cmp %o1, ':'
    bne colon_loop
    add %o0, 1, %o0
    ! ok, now we have start of minutes in %o0
    ! replace the colon by a zero
    ! and convert the minutes part
    call atoi
    stb %g0, [%o0-1]
    ! we now have the minutes in %o0, save it
    mov %o0, %i1
    ! convert the hours
    call atoi
    mov %i0, %o0
    ! we now have hours in %o0
    ! return with hours,minutes in %o0,%o1 respectively
    ret
    restore %o0, %g0, %o0

! simple atoi routine
! in: %o0 pointer to zero-terminated string of digits
! out: %o0 the converted value
atoi:
    mov %g0, %o1            ! start with zero
atoi_loop:
    ldub [%o0], %o2         ! load next character
    cmp %o2, 0              ! end of string?
    be atoi_done
    sub %o2, '0', %o2       ! adjust for ascii
    ! now multiply partial result by 10, as 8x+2x
    sll %o1, 3, %o3         ! 8x
    sll %o1, 1, %o1         ! 2x
    add %o1, %o3, %o1       ! 10x
    ! now add current digit and loop back
    add %o2, %o1, %o1
    b atoi_loop
    add %o0, 1, %o0
atoi_done:
    retl
    mov %o1, %o0
Jester
  • 56,577
  • 4
  • 81
  • 125
  • thanks for the answer, can you please give me details? I am not really sure how to approach this problem – Andy M Feb 03 '13 at 01:16
  • Updated with sample implementation. – Jester Feb 03 '13 at 02:57
  • thanks a lot, also you wrote "find me as Jester01 on irc.freenode.net" on your profile but that website doesn't work, is there any other way to contact you ? – Andy M Feb 04 '13 at 00:36
  • That's not a website, that's an Internet Relay Chat server. You can use the [web interface](http://webchat.freenode.net/) if you don't have a client. – Jester Feb 04 '13 at 00:58