homepage Welcome to WebmasterWorld Guest from 23.20.61.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
Forum Library, Charter, Moderators: bakedjake

Linux, Unix, and *nix like Operating Systems Forum

    
Finding last char when unknown
Finding the last character when unknowin in char string
akande

10+ Year Member



 
Msg#: 834 posted 7:22 pm on Oct 29, 2003 (gmt 0)

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

10+ Year Member



 
Msg#: 834 posted 8:26 pm on Oct 29, 2003 (gmt 0)

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+ Year Member



 
Msg#: 834 posted 10:17 pm on Oct 29, 2003 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved