Hailey
Hailey

Reputation: 157

Convert the C function into ARM assembly language

How exactly do I convert this C program into assembly code? I am having a hard time understanding the unsigned int manipulation.

unsigned int sum(unsigned int n){
     if(n==0) return 0;
     else return n+sum(n-1);
   }

I have done this if I consider int.How to think for unsigned int?

sum:
SUB sp, sp, #8
STR lr, [sp,#4]
STR r0, [sp,#0]
CMP r0,#0
BGE L1
MOV r0, #0
ADD sp, sp, #8
MOV pc, lr
L1: SUB r0, r0, #1
BL sum
MOV r12, r0
LDR r0, [sp,#0]
LDR lr, [sp,#4]
ADD sp, sp, #8
ADD r0, r0, r12
MOV pc, lr

Upvotes: 2

Views: 1194

Answers (1)

Mostafa
Mostafa

Reputation: 468

It won't matter for unsigned int, instructions as ADD and SUB behave correctly in both.

Some ISAs provide unsigned ADD and SUB (ADDU and SUBU) as MIPS, which only differ in overflow behavior.

Upvotes: 1

Related Questions