Arm/structfind

From ASMBits

Consider the following struct:

struct ProfitList {
	unsigned int product_id;
	int profit;
};

An array of this struct can be used as a list of products (each with a product id) and the profit for selling each item.


struct ProductInfo {
	unsigned int product_id;
	char *product_name;
};

An array of struct ProductInfo maps each product_id with its name. For this problem, it is not important whether product_name is null or actually points to a valid string.

Given an array of ProfitList, find the product with the maximum profit (profit can be negative). There will be at least one entry in this array. If there is more than one product with maximum profit, return the first one.

Then, look up the product_id in an array of ProductInfo and return the product_name for that product. If the maximum-profit product isn't in the ProductInfo list, return 0.


char* find (struct ProfitList* profit_array, unsigned int profit_array_entries, struct ProductInfo* info_array, unsigned int info_array_entries);


Expected solution length: Around 25 lines.

Sample Input

[See sample input below]

Sample Output

r0=0x210

Write your solution here

x
 
1
// Some input data and testing code
2
.data
3
ProdList: 
4
.word 0x1000, 10
5
.word 0x1001, 11    // 11 is the maximum profit. Product_id=0x1001
6
.word 0x1002, 9
7
8
ProdInfo:
9
.word 0x1000, 0x200
10
.word 0x1001, 0x210 // Product_id 0x1001 has name 0x210. Return 0x210.
11
.word 0x1002, 0x220
12
13
14
.text
15
.global _start
16
_start:
17
    ldr r0, =ProdList
18
    ldr r1, =3
19
    ldr r2, =ProdInfo
20
    ldr r3, =3  
21
    bl find
22
    
23
    1: b 1b  // done
24
25
// Your function starts here:
26
find:
27
    
28
    
Upload a source file...