Machine Language -
1. Which of the following assembly code represents the high-level Java code below?
int x;
int i = 5;
if (i < 0)
x = -1;
else
x = 1;
D=M
@i
M=D
@BRANCH
M;JLT
@x
M=1
@END
0;JMP
(BRANCH)
@x
M=-1
(END)
@END
0;JMP
D=M
@i
M=D
@BRANCH
M;JGE
@x
M=1
@END
0;JMP
(BRANCH)
@x
M=-1
(END)
@END
0;JMP
D=M
@i
M=D
@BRANCH
M;JLT
@x
M=1
@END
0;JMP
(BRANCH)
@x
M=-1
(END)
@END
0;JMP
D=M
@i
M=D
@BRANCH
M;JGE
@x
M=1
@END
0;JMP
(BRANCH)
@x
M=-1
(END)
@END
0;JMP
2. Write the assembly code that performs the following high-level Java code below:
int n = 5;
int[] arr = new int[n]; // Assume the array points to memory location 100
for (int i = 0; i < n; i++)
arr[i] = 1;
1. No correct answer (option a and c are same here, some mistake. 2nd line code should be D=A)
@5 //A=5
D=A // D=5
@i //A=address of i
M=D //i=5
@BRANCH
M;JLT //if i<0 gto branch
@x
M=1 //x=1
@END
0;JMP //goto END
(BRANCH) //if i<5
@x
M=-1 //x=-1
(END)
@END
0;JMP //stop
Qn2)
@5
D=A
@n
M=D //n=5
@i
M=0 //i=0
(LOOP)
@i
D=M
@n
D=D ‐ M
@END
D; JGE //if (i-n)>=0 goto end
@arr
D=M
@i
A=D+M
M= 1 //arr[i]=1
@i
M=M+1 //i=i+1
@LOOP
0; JMP //goto (LOOP)
(END)
@END
0;JMP
Using pointer concepts , arr points to memory address
@5
D=A
@n
M=D //n=5
@100
D=A
@arr
M=D //arr points to address 100
@i
M=0 //i=0
(LOOP)
@i
D=M
@n
D=D ‐ M
@END
D; JGE //if (i-n)>=0 goto end
@arr
A=M //address in arr stored in A
M=1 //Mem[address in arr]=Mem[100]=1 in first iteration
@arr
M=M+1 //arr=arr+1, arr values increments to 101,102 etc in each iteration.
@i
M=M+1 //i=i+1
@LOOP
0; JMP //goto (LOOP)
(END)
@END
0;JMP
Get Answers For Free
Most questions answered within 1 hours.