1

So, my goal is the write a subroutine that when called hangs until the user has entered a string terminated by <return>, which it then returns(probably by writing it to an address specified by the user).

My problem lies in how i best get individual characters from the keyboard. As I see it there are 3 ways:

  1. Listen for interrupts from the keyboard and get the character in response to those. This would mean that the logic performed by getline would be in the interrupt handler, which seem to cause some problems. i.e. how do you return from getline in response to a press on the <return> key? You don't have the return address handy when in the interrupt handler. Also the pattern of putting too much specific logic in the interrupt handler seems to me... wrong... even though I'm very inexperienced in low level coding.

  2. Just keep pulling the keyboard for key presses.

  3. Implementing the old 1.1 behavior with the interrupt handler, by loading all characters pressed into a circular buffer(possibly of length 1).

Some more perspective on these options would be nice.

Andreas Vinter-Hviid
  • 1,482
  • 1
  • 11
  • 27

1 Answers1

1

when you call your getline it should setup the interrupt handler so it adds the typed keys to buffer and updates a index

then start a busy loop until the end of the buffer has a new line and disable the interrupts from the keyboard

getline: 
set push B
set push X
;--coming from interrupt dispatch
SET B, buffer ;--address from argument
SET C, bufferlength ;-- from argument
SET PUSH, 0
SET X, SP ;--address of counter
SET A, startKeyInput
INT softwareInterrupt

IAQ 1;--restart interrupts
startloop:
SET A,buffer
ADD A,SP
IFN [A],'\n'
    set PC, startloop ;--busy loop
IFL PEEK, X
    set PC, startloop ;-- stopping buffer overflow
IAQ 0;--stop interrupts
set A, stopKeyInput
INT softwareInterrupt

SET C,POP;-- C is return value
SET X,POP
SET B,POP
FRI ;-- from interrupt

and the interrupt handler adds the typed key to the buffer until it is full and adds 1 to the counter, this can be put in a interrupt handler itself but you'll need to reactivate interrupts while in the busy loop

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
  • I really appreciate your answer, but I'm afraid I don't quite understand all of it. I have written down some of the questions that popped up in my head while reading, but it got too long to be here. I have posted it here: http://pastebin.com/Nwq5qRSU If you feel like it, you are welcome to elaborate on some of the points in there. – Andreas Vinter-Hviid May 27 '12 at 19:27
  • @andvin if this answer works you should really accept the answer and study why it works. It's fairly clear to me what it does, perhaps you should start a more basic level of dcpu-16 assembly 0x10c Forum and 0x010c wiki have assemlby tutorials. – booyaa May 28 '12 at 08:07
  • 1
    @booyaa I do understand the basics of assembly, and what the instructions do. I am also capable of writing a working getline function. What I'm having trouble with is how best to do it. Judging from the non-trivial way ratchet freaks's implementation works, I suppose that some consideration went into designing it. I would really appreciate some explanation of these considerations. My problem is that i have no idea how to best utilize interrupts. What should the interrupt handler do and why? Things like this are AFAIK not explained in the typical DCPU tutorial. – Andreas Vinter-Hviid May 28 '12 at 13:39