Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Do While loop to do while loop #include <stdio.h> int racer1; int racer2; int

ID: 3575891 • Letter: 1

Question

1) Do While loop to do while loop

#include <stdio.h>
int racer1;
int racer2;
int main()
{
    racer1 = 0;
    racer2 = 0;
    scanf("%d", &racer1);
    scanf("%d", &racer2);
    do{
        racer1 += 20;
        racer2 += 25;
    }while(racer2 < racer1);
    printf("%d ",racer2);
    return 0;
}

2) Do While loop to for loop

#include <stdio.h>
int racer1;
int racer2;
int main()
{
    racer1 = 0;
    racer2 = 0;
    scanf("%d", &racer1);
    scanf("%d", &racer2);
    for(racer1; racer2 < racer1;){
        racer1 += 20;
        racer2 += 25;
    }
    printf("%d ",racer2);
    return 0;
}

3) Do While loop to while loop

#include <stdio.h>
int racer1;
int racer2;
int main()
{
    racer1 = 0;
    racer2 = 0;
    scanf("%d", &racer1);
    scanf("%d", &racer2);
    while ( racer2 < racer1 ) {
        racer1 += 20;
        racer2 += 25;
    }
    printf("%d ",racer2);
    return 0;
}

What I need to do is convert these three codes using Simple assembley language.

Instruction Format

Each line is independent and self-contained, and may be blank, an instruction, or a storage directive

All delimiters are the space character

Instructions are for an accumulator machine (left argument and result are in an implicit accu- mulator ACC register, except for COPY), with the following format

XXX arguments XXX is the reserved instruction name, required, in upper case arguments as needed separated by spaces

additional optional label can start any instruction (label:), just one label per line at most instructions (# arguments, meaning)

2.1 Instructions // I need to use this

• ADD (1, ACC = ACC +arg)
• BR (1, jump to arg)
• BRNEG(1,jumptoargifACC<0)
• BRZNEG (1, jump to arg if ACC <= 0) • BRPOS(1,jumptoargifACC>0)
• BRZPOS (1, jump to arg if ACC >= 0) • BRZERO (1, jump to arg if ACC == 0) • COPY (2, arg1 = arg2)
• DIV(1,ACC=ACC/arg)
• MULT (1, ACC = ACC * arg)
• READ (1, arg=input integer)
• WRITE (1, put arg to output as integer) • STOP (0, stop program)
• STORE (1, arg = ACC)
• SUB(1,ACC=ACC-arg)
• NOOP (0, nothing)
• LOAD (1, ACC=arg)

ADD, DIV, MULT, WRITE, LOAD, SUB can take either variable or immediate value as the arg: immediate value is positive integer or negative integer

• PUSH (0, tos++)
• POP (0, tos–)
• STACKW (1,stack[tos-arg]=ACC) • STACKR (1,ACC=stack[tos-arg])

PUSH/POP are only means to reserve/delete automatic storage.
STACKW/STACKR n - these are stack write/read instructions.
n must be a non-negative number, and the access is to nth element down from TOS, top of stack NOTE: tos points to the topmost element on the stack

Storage directives
XXX val
XXX is a name
val is the initial value
all storage and ACC size are signed 2 bytes

Storage name and label are all names starting with latter and following with letters and digits up to eight total

2.2 Assumptions

• any proper format within line, tokens separated by BS, blank space • all storage directives are listed following the last STOP
• all names start with letters and contain more letters or digits

3 Invocation

> virtMach // read from stdin
> virtMach file .asm // read from file .asm

So the simple assemply code should look something simillar to this

LOOP1: READ X

   LOAD X  

   BRZERO OUT1

   SUB 12

   BRZNEG LOOP1  

   SUB 7

   BRPOS LOOP1

   LOAD C

   ADD 1

   STORE C

   BR LOOP1

OUT1:   NOOP

   LOAD C

   WRITE C

   STOP

   C 0

   X 0  

Explanation / Answer

-------------------------------------------------------------------------------------------
1. Do While loop to Do while
-------------------------------------------------------------------------------------------

READ X
READ Y
BR L1

L1 :

LOAD X
ADD 20
STORE X
LOAD Y
ADD 25
STORE Y
SUB X
BRZPOS L2
BR L1

L2 :

WRITE Y
STOP
X 0
Y 0

Explanation :

Let racer1 be X and racer2 be Y. We are reading input values of X and Y, then calling the code block L1. Inside L1, value of X is first loaded in ACC and then 20 is added to it and the result is kept into the ACC. Then, the result from ACC is stored back into the variable X. Same is done with the variable Y. Now, we need to check for the condition (racer2 < racer1 ==> racer2-racer1<0 ==> Y-X < 0). We first substract value of X from Y (which is already in ACC) and see if the result is >=0 (BRZPOS). If the result is negative, then we continue the loop by branching to L1. If the result is positive, we got to L2, print out the value of Y and terminate.

-------------------------------------------------------------------------------------------
2. Do While loop to for loop
-------------------------------------------------------------------------------------------

READ X
READ Y
BR L1

L1 :

LOAD Y
SUB X
BRZPOS L2
LOAD X
ADD 20
STORE X
LOAD Y
ADD 25
STORE Y
BR L1

L2 :

WRITE Y
STOP
X 0
Y 0

-------------------------------------------------------------------------------------------
3. Do While loop to While loop
-------------------------------------------------------------------------------------------

READ X
READ Y
BR L1

L1 :

LOAD Y
SUB X
BRZPOS L2
LOAD X
ADD 20
STORE X
LOAD Y
ADD 25
STORE Y
BR L1

L2 :

WRITE Y
STOP
X 0
Y 0