JMooney5115
September 8th, 2008, 11:59 PM
I need to convert this C code into assembly. Here is the C:
int count, max, min, sum, average, remainder;
char *str={5, 0, 6, 1, 3};
main( )
{
min = str[0];
max = str[0];
sum = str[0];
for (count = 1; count < 5; count++)
{
if (min > str[count])
min = str[count];
if (max < str[count])
max = str[count];
sum = sum+str[count];
}
average = sum/5;
remainder = sum%5;
return;
}
So far I have for assembly:
ORG $1000
SUM: DS.W 1
COUNT: DS.W 1
MIN: DS.W 1
MAX: DS.W 1
AVG: DS.W 1
REM: DS.W 1
STR: DC.B 5, 0, 6, 1, 3
ORG $2000
LDAA STR
STAA MIN
STAA MAX
STAA SUM
FOR_1: LDAB COUNT
LDY COUNT
LDAA STR, Y
STAA REM
CMPB #5
BGT COUNT;;;;branch out of range [-128..127]
if LDAA STR, Y ;;;load string +count into a
STAA REM;;;store string + count into a
CMPA MIN;;;compare min;;;;
BLT MIN;;;lessthan min;;;branch out of range [-128..127]
;;;;LDD REM
;;;;ADDD REM
;;;;STD MIN
STA MIN;min=str[count]
else LDAA STR, Y
STAA REM
CMPA MAX;;;
BGT MAX;;;branch out of range [-128...127]
;;;;LDD REM
;;;;ADDD REM
;;;;STD MAX
STA MAX;;;max=str[count]
;;;;END IF_2
LDD SUM
ADDD REM
STD SUM
INC COUNT;;;increase count by 1
BRA FOR_1;;;ends for loop
av: LDY COUNT;;;load count into y
LDAA STR, Y;;; store str+count into a
STAA REM;;; stores a into rem
LDD SUM;;; load sum into d
ADDD REM;;; add rem to sum
STD SUM
;;;does the division
LDD SUM
LDX #5
IDIV
STX AVG
STD REM
RTS
END
Do I have this correct? I do not know how to step through the array to make everything at the char[cnt]. Can someone help explain this to me please?
Thanks,
Mooney
int count, max, min, sum, average, remainder;
char *str={5, 0, 6, 1, 3};
main( )
{
min = str[0];
max = str[0];
sum = str[0];
for (count = 1; count < 5; count++)
{
if (min > str[count])
min = str[count];
if (max < str[count])
max = str[count];
sum = sum+str[count];
}
average = sum/5;
remainder = sum%5;
return;
}
So far I have for assembly:
ORG $1000
SUM: DS.W 1
COUNT: DS.W 1
MIN: DS.W 1
MAX: DS.W 1
AVG: DS.W 1
REM: DS.W 1
STR: DC.B 5, 0, 6, 1, 3
ORG $2000
LDAA STR
STAA MIN
STAA MAX
STAA SUM
FOR_1: LDAB COUNT
LDY COUNT
LDAA STR, Y
STAA REM
CMPB #5
BGT COUNT;;;;branch out of range [-128..127]
if LDAA STR, Y ;;;load string +count into a
STAA REM;;;store string + count into a
CMPA MIN;;;compare min;;;;
BLT MIN;;;lessthan min;;;branch out of range [-128..127]
;;;;LDD REM
;;;;ADDD REM
;;;;STD MIN
STA MIN;min=str[count]
else LDAA STR, Y
STAA REM
CMPA MAX;;;
BGT MAX;;;branch out of range [-128...127]
;;;;LDD REM
;;;;ADDD REM
;;;;STD MAX
STA MAX;;;max=str[count]
;;;;END IF_2
LDD SUM
ADDD REM
STD SUM
INC COUNT;;;increase count by 1
BRA FOR_1;;;ends for loop
av: LDY COUNT;;;load count into y
LDAA STR, Y;;; store str+count into a
STAA REM;;; stores a into rem
LDD SUM;;; load sum into d
ADDD REM;;; add rem to sum
STD SUM
;;;does the division
LDD SUM
LDX #5
IDIV
STX AVG
STD REM
RTS
END
Do I have this correct? I do not know how to step through the array to make everything at the char[cnt]. Can someone help explain this to me please?
Thanks,
Mooney