homepage Welcome to WebmasterWorld Guest from 54.167.144.4
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

    
Sparc Assembly Help!
matticus1181




msg:3518862
 8:48 pm on Dec 2, 2007 (gmt 0)

I'm taking a class in sparc assembly, and I'm having a hard time with this concept. We're supposed to add two numbers together that can have as many as 100 digits. Since this is too many to fit into one register, I have to save them in memory and then deal with them. This is the part that I can't figure out. When saving it, should I use %d or %s? I'm saving the input in a variable that I declared like this: input1:.skip 4 * 50. Also, I was planning on using addx and addxcc to keep the carry and was planning on loading up 32 bits at a time and adding that and then restoring it, but I can't figure it out.

Am I going about this in the right way? Does anyone have any suggestions? Thanks in advance!

 

jdMorgan




msg:3518898
 10:23 pm on Dec 2, 2007 (gmt 0)

It's been years since I did any SPARC work, and the details of the assembler directives are forgotten, but your approach is sound:

Add the least-significant addends together, generating and saving the carry bit. Do not use the previous value of the carry bit in this initial addition operation -- clear it if necessitated by the instruction set.

Then add the next-least-significant words together both using the carry from the previous addition, and generating carry for the next addition.

Continue working your way through the most-significant words. If adding those most-significant words generates a carry, don't forget to include it in the final result! -- Well, unless it exceeds the number of bits you're allowed to output, in which case you should call it an error.

I assume that the input and output numbers don't need to be converted from/to decimal, and that both input numbers are positive.

Jim

matticus1181




msg:3518906
 10:45 pm on Dec 2, 2007 (gmt 0)

Thanks for the quick response. This is what I had come up with before:


.section ".data"
format: .asciz "%s%c"
displayformat: .asciz "%d\n"
prompt1: .asciz "Enter first number (100 digit max): "
prompt2: .asciz "Enter second number (100 digit max): "
sDisplaySum: .asciz "\nThe sum is: %d"
sDisplayDiff: .asciz "\nThe difference is: %d"
nl: .byte '\n'


.align 4
.section ".bss"
input1: .skip 4 * 50
input2: .skip 4 * 50
sum: .skip 4 * 50
diff: .skip 4 * 50


.align 4
findSumDiff:
save %sp, -96, %sp!can not be leaf b/c must call validate subroutine


!use a loop to continue to load .words and check each time to see if %l1 and %l2 are 0
!use addxcc and
mov 0, %l3!set byte counter to loop


set input1, %l0!load first 32 bits to add
ld [%l0 + %l3], %l1!first number


set input2, %l0!load first 32 bits to add
ld [%l0 + %l3], %l2!second number


addcc %l1, %l2, %l4!does first add and sets the carry flag


set sum, %l5
st %l4, [%l5 + %l3]!store the first part of the sum into memory


add %l3, 4, %l3!increment counter


addloop:
set input1, %l0!loads the next 32 bits of first number
ld [%l0 + %l3], %l1


set input2, %l0!loads the next 32 bits of second number
ld [%l0 + %l3], %l2


addxcc %l1, %l2, %l4!adds in carry to the addition, adds, then sets the flag again


set sum, %l5
st %l4, [%l5 + %l3]!store the first part of the sum into memory


add %l3, 4, %l3!increments counter again


cmp %l3, 200
bne addloop
nop


ret
restore

I was thinking I was having a problem with the load and store part of things... What do you think?

jtara




msg:3519871
 3:54 am on Dec 4, 2007 (gmt 0)

Sparc is one assembly language I've never done - it sure does look weird! (And among the ones that I *have* done is IBM 360... I thought *that* was weird-looking...)

So, I can't help you with whether your code works or not (Don't you have access to a machine to debug it on? If not, you should be able to get an emulator.)

But I am suspicious of the word "digit" in the description of the problem.

Are you sure you aren't supposed to be doing BCD arithmetic, rather than binary?

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