Question

In: Computer Science

I want to convert those codes to assembler code // Problem 1 // for loop J=5...

I want to convert those codes to assembler code

// Problem 1

// for loop

J=5

for(i=1; i<5; i++) {

        j--

}

// Problem 2

// if - then - else

i=4

if (i < 5) then

        j = 3

else

        j = 2

// Problem 3

//while loop

i = 0

j = 0

while(i==0) {

  j++

  if j = 5 then

        i = j

}

Solutions

Expert Solution

1) Simply we write mov instruction for assigning values to a variable.

Mov instruction : mov destination,source.

Given problem :
//for loop
j=5
for(i=1; i<5; i++) {
        j--
}

Given code can be converted to assembley code as follows :

pushq    %rbp
    movq    %rsp, %rbp
    movl    $5, -8(%rbp) ;j=5
    movl    $1, -4(%rbp) ;i=1
    jmp    .L2
.L3:
    subl    $1, -8(%rbp)
    addl    $1, -4(%rbp)
.L2:
    cmpl    $4, -4(%rbp) ;condition check - i<5
    jle    .L3
    popq    %rbp
    ret



2) Given problem :

i=4
if (i < 5) then
        j = 3
else
        j = 2

This code can be converted to assembley code as follows :

    pushq    %rbp
    movq    %rsp, %rbp
    movl    $4, -8(%rbp) ;assignment instruction which is equivalent to i =4i=4
    cmpl    $4, -8(%rbp) ;comparing whether i < 5 or not
    jg    .L2
    movl    $3, -4(%rbp)
    jmp    .L3
.L2:
    movl    $2, -4(%rbp)
.L3:
    popq    %rbp
    ret

3) Given code :

i = 0
j = 0
while(i==0) {
j++
if j = 5 then
        i = j
}

Above code can be converted to assembley code as follows :

    pushq   %rbp
   movq   %rsp, %rbp
   movl   $0, -8(%rbp)
   movl   $0, -4(%rbp)
   jmp   .L2
.L3:
   addl   $1, -4(%rbp) ;increment of j
   cmpl   $5, -4(%rbp)
   jne   .L2
   movl   -4(%rbp), %eax
   movl   %eax, -8(%rbp)
.L2:
   cmpl   $0, -8(%rbp) ;while condition check
   je   .L3
   popq   %rbp
   ret
   
   


Related Solutions

Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding...
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding in NASM on a SASM system.
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
Write the following code in ARM assembly code g=12, h=8, i=2, j=5; f = (g +...
Write the following code in ARM assembly code g=12, h=8, i=2, j=5; f = (g + h) - (i + j); Your program displays the message: f = (g + h) – (i + j) = 13 Note that 13 should be calculated, not hardcoded
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
1- Block Codes consist of a combination of a sequence code and a mnemonic code:
  1- Block Codes consist of a combination of a sequence code and a mnemonic code: True False 2- Business process reengineering efforts sometimes fail because a- Employees will never accept change b- Management gets too involved in the process c- Management support can never overcome employee resistance d- Management is too optimistic regarding its expectations from its implementation 3- RFIDS allow firms a- Track inventory more effectively b- Eliminate the use of databases c- Reduce the costs of ERP...
3. Let S3 act on the set A={(i,j) : 1≤i,j≤3} by σ((i, j)) = (σ(i), σ(j))....
3. Let S3 act on the set A={(i,j) : 1≤i,j≤3} by σ((i, j)) = (σ(i), σ(j)). (a) Describe the orbits of this action. (b) Show this is a faithful action, i.e. that the permutation represen- tation φ:S3 →SA =S9 (c) For each σ ∈ S3, find the cycle decomposition of φ(σ) in S9.
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
PROBLEM 1-9. Ethics and Code of Conduct [LO 5] Search the Web for the Code of...
PROBLEM 1-9. Ethics and Code of Conduct [LO 5] Search the Web for the Code of Conduct for Sears Holdings Corporation (the parent company for Sears and Kmart). For each of the following cases, indicate whether it is allowable under the Code of Conduct. In your answer, cite specific language in the code. a. A purchasing agent is negotiating with two suppliers A and B. The agent tells supplier B the bid price offered by supplier A in an effort...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT