1. Homepage
  2. Programming
  3. Computer Programming Project Task 2: Stack up!

Computer Programming Project Task 2: Stack up!

Engage in a Conversation
NetherlandsVUVrije Universiteit AmsterdamComputer ProgrammingC dreaded BinariesStack up!

2 - Stack up!

What to implement

Functions

  • get_program_counter
  • tos
  • step
  • finished

Instructions

  • OP_BIPUSH
  • OP_DUP
  • OP_IADD
  • OP_IAND
  • OP_IOR
  • OP_ISUB
  • OP_NOP
  • OP_POP
  • OP_SWAP
  • OP_ERR
  • OP_HALT
  • OP_OUT
  • OP_IN

In order to pass test2, correctly implement all of the functions and instructions listed above. CourseNana.COM

Introduction

In this chapter, you will implement the basic stack operations of the IJVM, such as popping two words from the stack, adding them and then pushing the result (OP_IADD). CourseNana.COM

All IJVM stack operations operate on words, so the implemented stack should also operate on words. CourseNana.COM

The program counter

The program counter is an index in the text data that points to the current instruction. It should be initialised to 0 at the start of your program and incremented accordingly with each executed instruction. CourseNana.COM

The suggested approach

Start with implementing the stack, as all the functions and instructions depend on it. The simplest and fastest way to implement the stack is to simply allocate an array for the data on the stack and keep track of its maximum capacity and current size. As a reminder: a stack is a LIFO (last in, first out) data structure with three main principal operations: CourseNana.COM

  • push: add an element to the top of the stack
  • pop: remove the element on the top of the stack and return it
  • top: return the element on the top of the stack

Once you've implemented the stack, shift your focus to the step function. The step function executes the current instruction (i.e., the instruction pointed to by the program counter). Consequently, your step function must be able to handle all the instructions required in order to pass test2. CourseNana.COM

Each consecutive chapter will introduce a new set of instructions for you to implement, so your emulator will eventually be able to handle the entire IJVM instruction set. It is a good idea to use a switch statement to determine how to handle the current instruction. CourseNana.COM

The instructions for task 2 are: CourseNana.COM

OpCodeInstructionArgsDescription
0x10BIPUSHbytePush a (signed) byte onto the stack
0x59DUPN/ADuplicate top of stack: Push a copy of the top word onto the stack
0x60IADDN/APop two words from the stack and push their sum
0x7EIANDN/APop two words from the stack and push their bitwise AND
0xB0IORN/APop two words from the stack and push their bitwise OR
0x64ISUBN/APop two words from the stack and push their difference Note: subtract the top word from the second to top word
0x00NOPN/ADo nothing
0x57POPN/APop a word from the stack
0x5FSWAPN/ASwap the two top words on the stack
0xFEERRN/APrint an error message and halt the emulator
0xFFHALTN/AHalt the emulator
0xFCINN/ARead a character (byte) from the machine input and push it onto the stack. If no character is available, push 0 instead.
0xFDOUTN/APop a word from the stack and print it to the machine output as a character

Hints

  • Please refer to the ijvm.h header file for a description of each function in machine.h CourseNana.COM

  • Since you are going to use a stack, it makes sense to implement a stack data type (i.e., a struct). CourseNana.COM

  • Remember to add some basic error handling to your code (i.e., assert that the stack is not empty when pop is called). Adding these kinds of checks will help you a lot down the line. CourseNana.COM

  • It is a good idea to enlarge your stack if it's full when push is called. You can use realloc for example. CourseNana.COM

  • To implement the IN instruction you have to read a byte from the in file description, which you can do with fgetc(in). If no character is avaible, fgetc will return EOF. Similarly, to write a byte value to out, use fprintf(out, "%c", value). CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Netherlands代写,VU代写,Vrije Universiteit Amsterdam代写,Computer Programming代写,C代写, dreaded Binaries代写,Stack up!代写,Netherlands代编,VU代编,Vrije Universiteit Amsterdam代编,Computer Programming代编,C代编, dreaded Binaries代编,Stack up!代编,Netherlands代考,VU代考,Vrije Universiteit Amsterdam代考,Computer Programming代考,C代考, dreaded Binaries代考,Stack up!代考,Netherlandshelp,VUhelp,Vrije Universiteit Amsterdamhelp,Computer Programminghelp,Chelp, dreaded Binarieshelp,Stack up!help,Netherlands作业代写,VU作业代写,Vrije Universiteit Amsterdam作业代写,Computer Programming作业代写,C作业代写, dreaded Binaries作业代写,Stack up!作业代写,Netherlands编程代写,VU编程代写,Vrije Universiteit Amsterdam编程代写,Computer Programming编程代写,C编程代写, dreaded Binaries编程代写,Stack up!编程代写,Netherlandsprogramming help,VUprogramming help,Vrije Universiteit Amsterdamprogramming help,Computer Programmingprogramming help,Cprogramming help, dreaded Binariesprogramming help,Stack up!programming help,Netherlandsassignment help,VUassignment help,Vrije Universiteit Amsterdamassignment help,Computer Programmingassignment help,Cassignment help, dreaded Binariesassignment help,Stack up!assignment help,Netherlandssolution,VUsolution,Vrije Universiteit Amsterdamsolution,Computer Programmingsolution,Csolution, dreaded Binariessolution,Stack up!solution,