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

this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
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; }
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
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.
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT