Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Programming Studio-2 COSC2804

Assignment 2

1 Overview

This assignment will assess learning goals in computer architecture, focusing on assembly code programming. The tasks include:

a)  Writing programs in LC-3 assembly language to demonstrate your understanding of loops, branches, subroutines, two’s complement, and traps.

b)  Converting between hexadecimal, binary and assembly representations of an LC-3 program.

c)   Simulating a computer architecture via an existing Little Computer 3 (LC-3) virtual machine, written in Python.

d)  Configuring all the required development tools (Minecraft Pi Edition, Visual Studio Code + LC-3 extension, laser, Git, etc.)

The assignment is to be completed individually.

2 Learning Outcomes

This assessment relates to the following learning outcomes:

[CLO2]: Apply fundamentals of computer architecture, operating systems, and system deployment to the design and development of medium-sized software applications.

[CLO4]: Demonstrate skills for self-directed learning, reflection, and evaluation of your own and your peers work to improve professional practice.

[CLO5]: Demonstrate adherence to appropriate standards and practice of Professionalism and Ethics.

3 Preliminaries

Like Assignment 1, this assignment involves placing blocks in Minecraft programmatically. This time, however, you will see that building basic structures with LC-3 is far more challenging than building the same structures with Python!

The below sections will get you started with creating an LC-3 development environment and understanding how to access MCPI commands in our modified LC-3 virtual machine.

Setting up the tools required for carrying out this assignment.

To work on Assignment 2, you’ll first need to:

•   Install the VS Code LC-3 extension.

•   Install laser, which is needed to assemble your code.

•   Initialise a GitHub Classroom repo for Assignment 2. The initial repo contains all necessary starter code, including a modified LC-3 virtual machine written in Python.

Setup instructions can be found in the course Canvas shell. See the module Getting Started with LC-3”, which is linked on the front page.

Communicating with Minecraft via LC-3.

LC-3 is a very simple language that offers no native way of accessing MCPI functions. To get         around this, we provide a modified LC-3 virtual machine that contains additional “trap” subroutines for communicating with Minecraft.

These traps are outlined in the table below:

Function

Trap Vector

printRegisters()

0x30

mc.postToChat(R0)

0x31

mc.player.getTilePos() --> R0, R1, R2

0x32

mc.player.setTilePos(R0, R1, R2)

0x33

mc.getBlock(R0, R1, R2) --> R3

0x34

mc.setBlock(R0, R1, R2, R3)

0x35

mc.getHeight(R0, R2) --> R1

0x36

Notes:

•   printRegisters() is provided purely for debugging purposes, since unlike the LC-3 web  simulator that we’ve used in class, the Python-based virtual machine does not provide an easy way of inspecting the current register values.

•   Function arguments and return values are passed via the registers. For example, TRAP 0x36  (corresponding to mc.getHeight) assumes that the x and z arguments are passed via registers R0 and R2 respectively, and outputs the return value to R1.

•   You may notice that setBlocks is missing from the list of functions above. This is deliberate!

•   You must not modify the provided virtual machine in any way. The only files in the starter repo that you should edit are the *.asm files, problem_ 12.txt, and README.md.

4 LC-3 Programming Challenges

Using traps, branches and logical operators (8 marks: 2 marks per problem)

1)  Write an LC-3 assembler program places a gold block (block ID #41) just above ground level,

4 units from the player in the z direction.

a)  Place your code in the assembly file “place_gold.asm” .

b)  The x-coordinate of the gold block should match the player’s x-coordinate.

c)   See Figure 1 for an illustration.

Figure 1: An example illustrating where the gold block should be placed for Problem 1.

2)  Write an LC-3 assembler program that checks the ID of the block underneath the player, then outputs a message to Minecraft chat depending on the block ID, as explained below.

a)  Place your code in the assembly file “check_block.asm” .

b)  If the block beneath the player’s tile position is stone (block ID #1), the program should output “The block beneath the player tile is stone” .

c)   If the block beneath the player’s tile position is grass (block ID #2), the program should output “The block beneath the player tile is grass” .

d)  Otherwise, the program should output “The block beneath the player tile is neither stone nor grass” .

e)  Note that the output messages should be sent to Minecraft chat, not the console!

3)  Write an LC-3 assembler program that reads the player’s current tile position:

(playerPos.x, playerPos.y, playerPos.z)

then teleports the player to:

(-playerPos.z, 3 * playerPos.y, |playerPos.x|)

where |playerPos.x| denotes the absolute value of playerPos.x.

a)  Place your code in the assembly file “teleport.asm” .

b)  The player may end up somewhere bad”, e.g., in the middle of a mountain. You do not need to worry about handling this.

4)  Write an LC-3 assembler program that calculates the bitwise OR between two blocks (based on their block IDs) then outputs a new block representing the result.

a)  Place your code in the assembly file “bitwise_or.asm” .

b)  The input block IDs should be read from:

(playerPos.x + 1, playerPos.y – 1, playerPos.z)

and

(playerPos.x + 2, playerPos.y – 1, playerPos.z)

c)   The output block should be placed at:               (playerPos.x + 3, playerPos.y – 1, playerPos.z)

d)  See Figure 2 for an illustration. Note: You may find it useful to create a Python script that creates test inputs for the LC-3 program, since the default Minecraft terrain is unlikely to  provide useful test cases.

Figure 2: An example of how the bitwise OR program should behave.

Loops and subroutines (6 marks: 2 marks per problem)

5)  Write an LC-3 assembler program that scans a line of blocks and replaces them with new blocks where the block IDs have been incremented by 1.

a)  Place your code in the assembly file “increment_blocks.asm” .

b)  The assembly file contains a predefined constant, LINE_LEN, that specifies the length of the line of blocks to replace. Your code should work for all non-negative values of LINE_LEN (including LINE_LEN = 0, which should leave the world unchanged).

c)   The line of blocks replaced should start at (playerPos.x + 1, playerPos.y – 1, playerPos.z) and end at (playerPos.x + LINE_LEN, playerPos.y – 1, playerPos.z).

d)  Do not worry about handling bad” blocks that do not stay in position, e.g., lava, water.

e)   See Figure 3 for an illustration.

Figure 3: Illustration of how the program for Problem 5 should behave if LINE_LEN = 5.

6)  Write an LC-3 assembler program that waits for the player to reach a particular location in the x-z plane, then terminates with the message goal reached” .

a)  Place your code in the assembly file “goal_check.asm” .

b)  The assembly file contains two predefined constants, GOAL_X and GOAL_Z, that specify the location of the goal. The program should still work if these values are changed.

c)   The program should terminate when playerPos.x = GOAL_X and playerPos.z = GOAL_Z.

d)  The termination message should be output to Minecraft chat.

7)  Write an LC-3 assembler program with a subroutine for creating a lamp” at a given location. Use this subroutine to create lamps at four locations, as specified below.

a)  Place your code in the assembly file “lamps.asm” .

b)  For the purposes of this problem, a lamp” at (x, y, z) is defined as:

i.   A stone block (block ID #1) at (x, y, z)

ii.   A stone block (block ID #1) at (x, y + 1, z)

iii.  A stone block (block ID #1) at (x, y + 2, z)

iv.   A glowstone (block ID #89) at (x, y + 3, z)

c)   The x, y and z arguments should be passed to the lamp-generating subroutine via designated registers (the choice of which registers is up to you).

d)  The subroutine should be used to place lamps at the following locations:

i.   (playerPos.x + 2, playerPos.y, playerPos.z + 2)

ii.   (playerPos.x + 2, playerPos.y, playerPos.z - 2)

iii.   (playerPos.x - 2, playerPos.y, playerPos.z + 2)

iv.   (playerPos.x - 2, playerPos.y, playerPos.z - 2)

e)   See Figure 4 for an illustration.

Figure 4: A third-person view of how the lamps should be placed around the player for Problem 7.

Advanced challenges (12 marks: 3 marks per problem)

8)  Write an LC-3 assembler program that places a rectangle of grass blocks underneath the player.

a)  Place your code in the assembly file “grass_rect.asm” .

b)  The assembly file contains two predefined constants, X_DIST and Z_DIST, that indirectly specify the dimensions of the rectangle (see Figure 5 for an illustration). Your code should work for all non-negative values of X_DIST and Z_DIST, including 0.

c)   The rectangle should be made of grass blocks (block ID #2).

d)  The rectangle should be centred 1 unit under the player tile, so that after the rectangle is created, the player is standing on top of it.

Figure 5: Illustration of the rectangles that should be built for X_DIST = 2, Z_DIST = 3 (on the left) and X_DIST = 0, Z_DIST = 2 (on the right).

9)  Write an LC-3 assembler program that checks whether the player is within a certain Euclidean distance of a specified goal” point.

a)  Place your code in the assembly file “euclidean_dist.asm” .

b)  The assembly file contains predefined constants, G_X, G_Y and G_Z, that specify the position of the goal point.

c)  An additional predefined constant, GOAL_DIST, specifies the distance bound to be checked.

d)  The Euclidean distance between the player and the goal point is given by:

deuClidean  =  √ (playerPos. x G_X)2  +  (playerPos. y G_Y)2  +  (playerPos. z G_Z)2

However, calculating this quantity is tricky in LC-3, because there is no square root      function. Therefore, instead of checking whether deuClidean  < GOAL_DIST, you should check the squared distances:

(playerPos. x − G_X)2  +  (playerPos. y G_Y)2  +  (playerPos. z G_Z)2  < GOAL_DIST2

If this inequality is met, the program should output “The player is within distance of the       goal” to Minecraft chat. Otherwise, it should output The player is outside the goal bounds” to Minecraft chat.

10) Write an LC-3 assembler program that builds a stairway next to the player. You must abide by the following constraints:

a)  Place your code in the assembly file “stairway.asm” .

b)  The stairway should be built from stone blocks (block ID #1).

c)   The assembly file contains three predefined constants, WIDTH, LENGTH and HEIGHT, that specify the dimensions of the stairway. The program should still work if these values are changed, but you can assume that the dimensions will always be strictly positive (> 0).

d)  One way to visualise the construction of the stairway is to imagine multiple layers” of blocks stacked on top of each another, as in Figure 6. Following this terminology:

The bottom layer of the stairway should have one corner at

(playerPos.x + 1, playerPos.y, playerPos.z + 1)

and another corner at

(playerPos.x + WIDTH, playerPos.y, playerPos.z + LENGTH).

The next layer up should have one corner at

(playerPos.x + 1, playerPos.y + 1, playerPos.z + 2)

and another corner at

(playerPos.x + WIDTH, playerPos.y + 1, playerPos.z + LENGTH).

and so on…

The top layer should have one corner at

(playerPos.x + 1, playerPos.y + HEIGHT - 1, playerPos.z + HEIGHT)

and another corner at

(playerPos.x + WIDTH, playerPos.y + HEIGHT - 1, playerPos.z + LENGTH).

Figure 6: Illustration of how a stairway with WIDTH = 2, LENGTH = 4 and HEIGHT = 3, can be built from three layers of blocks stacked on top of each other. The bottom layer is shown at the left; the top layer is shown at the right. Note: We will not test your program with nonsensical  values of LENGTH and HEIGHT (i.e., LENGTH < HEIGHT).

11) Write an LC-3 program that “terraforms” a 3x3 area in the x-z plane, centred underneath the player.

a)  Place your code in the assembly file “terraform.asm” .

b)  Let h denote the height of the land at the (x, z) location of the player. The height of the 3x3 area around the player should be levelled so that it all has a height of h.

c)  Locations with a land height of less than h should be built up to the required height by replacing all air blocks between the current land height and the desired level with grass blocks (block ID #2).

d)  Locations with a land height of more than h should be bulldozed down to the required         height by replacing all non-air blocks above the desired level with air blocks (block ID #0).

Understanding the LC-3 instruction set (4 marks)

For the below problem, please place your answers in the file problem_ 12.txt” .

12) Below is an excerpt from an LC-3 program, where the instructions are expressed in hexadecimal:

x5020

x1001

x1001

x103F

x127F

x03FB

a)   Convert the excerpt to its binary representation.

b)  Convert the excerpt to LC-3 assembler notation.

c)  What value will R0 hold by the end of the code segment if the initial value in R1 is:

i.     4

ii.      7

d)  Summarise what the code segment does in a single, succinct sentence.

5 Academic integrity and plagiarism (standard warning)

Academic integrity is about honest presentation of your academic work. It means acknowledging the work of others while developing your own insights, knowledge and ideas. You should take    extreme care that you have:

•   Acknowledged words, data, diagrams, models, frameworks and/or ideas of others you have quoted (i.e. directly copied), summarised, paraphrased, discussed or mentioned in your assessment through the appropriate referencing methods.

•   Provided a reference list of the publication details so your reader can locate the source if            necessary. This includes material taken from Internet sites. Ifyou do not acknowledge the sources of your material, you may be accused of plagiarism because you have passed off the work and ideas of another person without appropriate referencing, as if they were your own.

RMIT University treats plagiarism as a very serious offence constituting misconduct. Plagiarism covers a variety of inappropriate behaviours, including:

•   Failure to properly document a source.

•   Copyright material from the internet or databases.

•   Collusion between students.

For further information on our policies and procedures, please refer to the following: https://www.rmit.edu.au/students/student-essentials/rights-and-responsibilities/academic- integrity.

We will run code similarity checks.

6 Getting Help

There are multiple venues for getting help. The first places to look are Canvas, recordings, and the discussion forum on Teams. You are also encouraged to discuss any issues you have in class with your tutors. Please refrain from posting solutions to the discussion forum.

7 Marking Guide

The rubric that will be used to grade the assignment is viewable on Canvas. (See the bottom of the Assignment 2 page.)

Important note: Please do not rename any of the predefined constants in the *.asm files. Some components of the assignment will be autograded, and changing the names of the constants will mess up the autograding scripts!