Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. This is the second part of this kata series. First part is here.
  2.  
  3. We want to create an interpreter of assembler which will support the following instructions:
  4.  
  5. mov x, y - copy y (either an integer or the value of a register) into register x.
  6. inc x - increase the content of register x by one.
  7. dec x - decrease the content of register x by one.
  8. add x, y - add the content of the register x with y (either an integer or the value of a register) and stores the result in x (i.e. register[x] += y).
  9. sub x, y - subtract y (either an integer or the value of a register) from the register x and stores the result in x (i.e. register[x] -= y).
  10. mul x, y - same with multiply (i.e. register[x] *= y).
  11. div x, y - same with integer division (i.e. register[x] /= y).
  12. label: - define a label position (label = identifier + ":", an identifier being a string that does not match any other command). Jump commands and call are aimed to these labels positions in the program.
  13. jmp lbl - jumps to to the label lbl.
  14. cmp x, y - compares x (either an integer or the value of a register) and y (either an integer or the value of a register). The result is used in the conditional jumps (jne, je, jge, jg, jle and jl)
  15. jne lbl - jump to the label lbl if the values of the previous cmp command were not equal.
  16. je lbl - jump to the label lbl if the values of the previous cmp command were equal.
  17. jge lbl - jump to the label lbl if x was greater or equal than y in the previous cmp command.
  18. jg lbl - jump to the label lbl if x was greater than y in the previous cmp command.
  19. jle lbl - jump to the label lbl if x was less or equal than y in the previous cmp command.
  20. jl lbl - jump to the label lbl if x was less than y in the previous cmp command.
  21. call lbl - call to the subroutine identified by lbl. When a ret is found in a subroutine, the instruction pointer should return to the instruction next to this call command.
  22. ret - when a ret is found in a subroutine, the instruction pointer should return to the instruction that called the current function.
  23. msg 'Register: ', x - this instruction stores the output of the program. It may contain text strings (delimited by single quotes) and registers. The number of arguments isn't limited and will vary, depending on the program.
  24. end - this instruction indicates that the program ends correctly, so the stored output is returned (if the program terminates without this instruction it should return the default output: see below).
  25. ; comment - comments should not be taken in consideration during the execution of the program.
  26.  
  27. Output format:
  28. The normal output format is a string (returned with the end command).
  29.  
  30. If the program does finish itself without using an end instruction, the default return value is:
  31.  
  32. -1 (as an integer)
  33.  
  34. Input format:
  35. The function/method will take as input a multiline string of instructions, delimited with EOL characters. Please, note that the instructions may also have indentation for readability purposes.
  36.  
  37. For example:
  38.  
  39. program = """
  40. ; My first program
  41. mov a, 5
  42. inc a
  43. call function
  44. msg '(5+1)/2 = ', a ; output message
  45. end
  46.  
  47. function:
  48. div a, 2
  49. ret
  50. """
  51. assembler_interpreter(program)
  52. The above code would set register a to 5, increase its value by 1, calls the subroutine function, divide its value by 2, returns to the first call instruction, prepares the output of the program and then returns it with the end instruction. In this case, the output would be (5+1)/2 = 3.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement