Question

Write an equivalent C function and explain what it computes.

3. (10 marks) The following is the assembly language translation of a C function mystery: r3, [r0, #0] r3, #0 L3 ldrb cmp beq

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This code loops until it finds 0 in the array and returns it's index where it found 0 . Following is the equivalent c program.Here i considered the size of the array as 5.

#include <stdio.h>

//the following search function is the equivalent function for the above assembly code.
int search(int array[5])
{
int i=0;
for(i=0;i<5;i++)
{

//if the element is 0 return i
if(array[i]==0)
{
return i;
}
}
return -1;
}

int main()
{
int array[5]={1,2,3,4,0};
printf("%d",search(array));
return 0;
}

Memory(MB) : 1.5449269396973 Time(sec) : 0 Output: Copy 4.

Add a comment
Know the answer?
Add Answer to:
Write an equivalent C function and explain what it computes. 3. (10 marks) The following is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Section B - ARM Assembly Language (25 marks) An ARM instruction set summary is provided at...

    Section B - ARM Assembly Language (25 marks) An ARM instruction set summary is provided at the end of this paper 1. (5 marks) Consider the following assembly instruction STMFD r13!, (r5-6} Before executing this instruction, registers hold the following values: Register Value Register r9 Value r4 0x00400040 0x00000000 r5 r10 0x11223344 0x00800080 r6 0x55667788 r11 0x10001000 r7 0x99aabbcc r12 0x20002000 r8 exddeeff00 r13 ex40004000 What memory locations are affected after executing the above instruction? In a table, with a...

  • Question 2 ARM Assembly Language (25 marks) An ARM instruction set summary is provided at the...

    Question 2 ARM Assembly Language (25 marks) An ARM instruction set summary is provided at the end of this paper. (5 marks) Explain the difference between eor and eors instruction. Use an example to show why both forms are useful. а. b. (5 marks) Explain using an example what the "Idr r3, [r7,#4]" instruction does. c. (10 marks) The following is the assembly language generated by a C compile type mystery, %function mystery: args 0, pretend = 0, frame =...

  • 13) (4 pts) Squaring a number is useful in many other algorithms. This assembly routine calculates...

    13) (4 pts) Squaring a number is useful in many other algorithms. This assembly routine calculates the square of an unsigned integer. How long does this take in clock cycles (worst case). Assume ARM 32 bit instruction set and the integer is in R0 before we begin. AREA L.textl, CODE, READONLY PROC EXPORT square square MOV R1,RO; CMP R1, #0 ANDS R3,RO,R1 BNE skip ADD RO,R1, LSL R3; MOV R2,R2, LSL #1 CMP R2, #0 BNE loop BX LR loop...

  • Below is a C function and a first attempt at translating it into equivalent ARM subroutine....

    Below is a C function and a first attempt at translating it into equivalent ARM subroutine. However, while the ARM code assembles successfully, execution of it reveals that it regularly gets stuck within fact in what is apparently an infinite loop. Explain why and explain how you can repair the ARM code so that it returns correctly while preserving the recursive nature of the original C function. int fact(int n) {   if (n == 0) {     return 1;   } else...

  • Write C code that perform the same as the assembly code. Hint:data is an integer avariable in C. OUT is a name of a function:- //code part is the following.. L1 LDR R0, =0X006 ;The hexadecimal of 0110...

    Write C code that perform the same as the assembly code. Hint:data is an integer avariable in C. OUT is a name of a function:- //code part is the following.. L1 LDR R0, =0X006 ;The hexadecimal of 0110 is 0x006 L2 LDR R1, =data L3 LDR R2, [data] L4 AND R2, #0X006 L5 CMP R2, #0X006 L6 BLEQ OUT L7 ..................

  • Q4 (10 marks): What are the values in registers R1, R2 and R3 at the end...

    Q4 (10 marks): What are the values in registers R1, R2 and R3 at the end of this ARMv7 assembly program? MOV R1, #2 MOV R2, #10 MOV R3, #10 L1: CMP R1, R3 BGE L3 BL L2 ADD R1, R1, #3 BL1 L2: ADD R2, R2, #3 MOV R15, R14 L3: MUL R3, R1, R2 Q5 (10 marks): Please write ARMv7 assembly program for following C programs. Assume that variables are stored in memory and their addresses can be...

  • indicate what each line of code means 2. Explain how the following ARM program can be...

    indicate what each line of code means 2. Explain how the following ARM program can be used to determine whether a computer is big- endian or little-endian: MOV R0, #100 LDR R1, -0xABCD876 STR R1, [RO] LDRB R2, [R0, #1] R1-0xABCD876

  • Problem 5 (15pts): Describe what the following program is doing (Do not need to explain each line of instruction....

    Problem 5 (15pts): Describe what the following program is doing (Do not need to explain each line of instruction. Just show me the purpose of this code). .equ LEDS, Ox100000 10 # define LEDS Ox10000010 .text global start #base address of LEDS on DEO-Nano start: movia r2, LEDS movi r3, 0b00000001 movi r4, OX7FFF slli r4, r4, 3 add r4, r4, r4 load: movi r5, 0b10000000 loop: stw rs, o(r2) mov r6, ro count: addi r6, r6, 1 bne r6,...

  • Please convert this C function into ARM ASSEMBLY LANGUAGE CORTEX M (Must be cortex m) #include<...

    Please convert this C function into ARM ASSEMBLY LANGUAGE CORTEX M (Must be cortex m) #include<stdio.h> int ascii(char c) {                 return (int)c; } int computeMagicNumber(char* name) {                 int sum, i; //varibales                 sum = 0; //set sum to 0                 //calculate sum                 int N = sizeof(name)/sizeof(name[0]); //get name size                 for(i=0;i<N;i++)                                 sum = sum + (i+1)*ascii(name[i]);                                 return sum%1001; //return magic number MAIN FUNCTION : extern int computeMagicNumber(char *); int main(void) { char name[255]="Yusuf Ozturk"; int magic; magic=computeMagicNumber(name); printf("\n Magic number is %d\n",magic); return...

  • Write an assembly function equivalent to the following C function. If X[k] > K[k+1], it swaps...

    Write an assembly function equivalent to the following C function. If X[k] > K[k+1], it swaps X[k] and X[k+1] and returns 1; otherwise, it returns 0. You may use either conditional branch or conditional execution or their combination. int swap(unsigned short X[], int k) if (X[k] > X[k+1]) { short tmp = X[k]; X[k+1] = X[k]; X[k] = tmp; return 1; else { return 0;

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT