From 9065e2c59115466474d91727f29ae90938e01c1b Mon Sep 17 00:00:00 2001 From: CrazyEttin <> Date: Sun, 6 Jun 2021 11:18:15 +0300 Subject: [PATCH] Protect registers from changes within calls and rename newline to printcrlf for the sake of consistency. --- source/byte2hexstr.inc | 6 ++++++ source/cmpstr.inc | 15 +++++++++++++-- source/newline.inc | 11 ----------- source/printcrlf.inc | 18 ++++++++++++++++++ source/println.inc | 4 ++-- source/printstr.inc | 12 +++++++++++- source/readln.inc | 2 +- source/readstr.inc | 7 +++++++ source/system.asm | 2 +- 9 files changed, 59 insertions(+), 18 deletions(-) delete mode 100644 source/newline.inc create mode 100644 source/printcrlf.inc diff --git a/source/byte2hexstr.inc b/source/byte2hexstr.inc index df43b88..1dcdee3 100644 --- a/source/byte2hexstr.inc +++ b/source/byte2hexstr.inc @@ -2,6 +2,9 @@ byte2hexstr: +;Store the initial registers in the stack +pusha + ;Set a key for the hex digits mov si, .key @@ -26,6 +29,9 @@ inc di dec cx jnz .loop +;Load the initial registers from the stack +popa + ret .key: diff --git a/source/cmpstr.inc b/source/cmpstr.inc index 10597b0..5098175 100644 --- a/source/cmpstr.inc +++ b/source/cmpstr.inc @@ -2,6 +2,11 @@ cmpstr: +;Store the initial registers in the stack +pusha + +.loop: + ;Load characters mov al, [si] mov bl, [di] @@ -19,14 +24,20 @@ je .eq ;Repeat inc si inc di -jmp cmpstr +jmp .loop .neq: ;Clear the carry flag clc -ret +jmp .done .eq: ;Set the carry flag stc + +.done: + +;Load the initial registers from the stack +popa + ret \ No newline at end of file diff --git a/source/newline.inc b/source/newline.inc deleted file mode 100644 index 1dd912d..0000000 --- a/source/newline.inc +++ /dev/null @@ -1,11 +0,0 @@ -;Prints a newline - -newline: - -mov si, .newline -call printstr - -ret - -.newline: -db 0xd, 0xa, 0x0 diff --git a/source/printcrlf.inc b/source/printcrlf.inc new file mode 100644 index 0000000..88814fd --- /dev/null +++ b/source/printcrlf.inc @@ -0,0 +1,18 @@ +;Prints a newline + +printcrlf: + +;Store the initial registers in the stack +pusha + +;Print the newline +mov si, .crlf +call printstr + +;Load the initial registers from the stack +popa + +ret + +.crlf: +db 0xd, 0xa, 0x0 diff --git a/source/println.inc b/source/println.inc index 07e8509..86db73c 100644 --- a/source/println.inc +++ b/source/println.inc @@ -1,4 +1,4 @@ -;Prints a string from si until a null followed by a newline. +;Prints a string from si until a null, followed by a newline. println: @@ -6,6 +6,6 @@ println: call printstr ;Print a newline -call newline +call printcrlf ret diff --git a/source/printstr.inc b/source/printstr.inc index 71be922..4258d98 100644 --- a/source/printstr.inc +++ b/source/printstr.inc @@ -2,6 +2,11 @@ printstr: +;Store the initial registers in the stack +pusha + +.loop: + ;Load a character lodsb @@ -11,10 +16,15 @@ je .done ;Print the character mov ah, 0xe +mov bx, 0x0 int 0x10 ;Repeat -jmp printstr +jmp .loop .done: + +;Load the initial registers from the stack +popa + ret diff --git a/source/readln.inc b/source/readln.inc index d45d1e4..451b380 100644 --- a/source/readln.inc +++ b/source/readln.inc @@ -6,6 +6,6 @@ readln: call readstr ;Print a newline -call newline +call printcrlf ret diff --git a/source/readstr.inc b/source/readstr.inc index e9119ae..7422def 100644 --- a/source/readstr.inc +++ b/source/readstr.inc @@ -2,6 +2,9 @@ readstr: +;Store the initial registers in the stack +pusha + ;Store the input length in the stack mov ah, 0 push ax @@ -117,6 +120,10 @@ inc bl jmp .findend .done: + +;Load the initial registers from the stack +popa + ret .nextchar: diff --git a/source/system.asm b/source/system.asm index 3c3e49c..9feec80 100644 --- a/source/system.asm +++ b/source/system.asm @@ -2,11 +2,11 @@ jmp start ;Calls +%include "printcrlf.inc" %include "printstr.inc" %include "println.inc" %include "readstr.inc" %include "readln.inc" -%include "newline.inc" %include "cmpstr.inc" %include "byte2hexstr.inc"