Welcome to WebmasterWorld Guest from 54.146.240.181

Forum Moderators: bakedjake

Message Too Old, No Replies

Finding last char when unknown

Finding the last character when unknowin in char string

     

akande

7:22 pm on Oct 29, 2003 (gmt 0)

10+ Year Member



I am programming in Sparc assembly.

Does anyone know how to get the last character (last letter in char) when it is completely uknown?

The code to load the letter into a register would be

! Load last char into temporary
ldub [%i0 + %l1], %l4

%l1 being the letter position in the char. The problem is, the char can be any length so just using 4 for %l1 when there are 5 letters (a=0, b=1,c=2,d=3,e=4) would not work when there are over say 20 letters within the char.

marcs

8:26 pm on Oct 29, 2003 (gmt 0)

10+ Year Member



If by char you mean string and they are the usual 0 terminated string type, then try this... This is C code but you'll see what I mean :

loop=0;
while(string[loop]!=0)
loop++;

that puts loop at the end of the string. To get the last character of the
string :

lastchar=string[loop-1]

or
loop--;
lastchar=string[loop];

Been to long since I've done assembly coding (680x0) so I can't provide sample code that may translate to Sparc assembly better. Concept remains the same, hope this helps.

akande

10:17 pm on Oct 29, 2003 (gmt 0)

10+ Year Member



In terms of the following code:

The %l1 represents the letter position in the string. The program is supposed to get last letter of string, store it in a register, move the previous letters to the right. Then put the stored letter back into the string but at the beginning.

It's the grabbing of the last letter which is getting me.
----------------------------------------------------------

.global RotateStr

RotateStr:
save %sp, -128, %sp

! Used as the counter
clr %l0

! Used as main register storage
clr %l4

! Used for next slot of prev to last
clr %l6

! Used for next slot of sec prev to last
clr %l7

! Always going to be two
add %l2, 1, %l2
Loop:
! Load last char into temporary
ldub [%i0 + %l1], %l4

! Go to register previous to last
add %l1, -1, %l1

! Move the previous to last char into temp register
ldub [%i0 + %l1], %l6

! Restore the previous to last char into next slot
stb %l6, [%i0 + %l2]

! Go to register previous to second to last
add %l1, -1, %l1

! Move the previous to second to last char into temp register
ldub [%i0 + %l1], %l7

! Restore the previous second to last register into next slot
stb %l7, [%i0 + %l2]

! Make sure the first slot is empty
cmp %l1, %g0
be Loop

! Put the last char into the first slot
stb %l4, [%i0 + %l1]

ret
restore
----------------------------------------------------------

Hmm, what to do with this thing.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month