@@@ -*- mode: asm; asm-comment-char: ?@ -*- @@@ How simple is a screen text editor? @@@ Let’s suppose you have a main loop that gets keystrokes and @@@ updates the screen if necessary to correspond with a screen image @@@ you have somewhere, before running the actual core logic. The @@@ code of this main loop might be almost arbitrarily simple or @@@ complex; it depends entirely on your platform. On a simple @@@ platform, it might just poll an I/O port until a keystroke is @@@ ready, then read the keystroke before passing it to the core @@@ logic; the screen image in video RAM might be automatically shown @@@ on the display. Perhaps the whole main loop is ten machine @@@ instructions. At the other extreme, the main loop might need to @@@ accept multiple different kinds of network connections and @@@ cryptographically authenticate each one, then speak RDP over some, @@@ VNC over others, Telnet over others, and WebSockets over others. @@@ I would argue that whatever complexity involved in getting letters @@@ onto a screen and keystrokes into the core logic is not part of @@@ the complexity of writing a text editor, per se. So here I'm going @@@ to define a very simple calling convention. The editor's core @@@ logic subroutine gets invoked with three arguments. r0 contains @@@ the current input event, which for the time being is just a @@@ keystroke, in the form of an ASCII byte, unsigned. r1 is a @@@ pointer to the screen buffer, which is, as in traditional Forth @@@ systems, 64 bytes wide and 16 rows tall. r9 is a pointer to a data @@@ area where the editor's data lives; we can assume that it is @@@ zeroed at startup and big enough. r9, and as usual all registers @@@ r4-r15 except lr, must be preserved until return, but r0-r3 and ip @@@ can be clobbered. .syntax unified .cpu cortex-m4 .thumb .thumb_func .globl editor editor: push {r4, r5, r6, lr} mov r4, r9 @ r4 is our vars base cmp r0, #8 @ ^H beq backspace @@ insert printable character by default return: pop {r4, r5, r6, pc} backspace: ldr r2, [r4] @ cursor position on screen ands r3, r2, #63 @ extract column number @@ XXX bit field extract? beq return @ nothing to do if in col 0 @@ now we need to copy characters left, starting at col 0, @@ and decrement the cursor pos. @@ No, this is totally wrong, we should delete to end of line. @@ From cursor. Not from beginning of line to cursor. eor r0, r2, r3 @ compute index of col 0 add r0, r1 @ add screen buffer base address 1: ldrb r5, [r0, #1]! @ load byte, incrementing pointer strb r5, [r0, #-1] @ store in previous byte subs r3, #1 @ Decrement loop counter. bne 1b @ Possibly repeat. subs r2, #1 @ Decrement cursor. strb r2, [r4] b return