OpenCores
URL https://opencores.org/ocsvn/forwardcom/forwardcom/trunk

Subversion Repositories forwardcom

[/] [forwardcom/] [libraries/] [gets_light.as] - Blame information for rev 108

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 90 Agner
/****************************  gets_light.as **********************************
2
* Author:        Agner Fog
3
* date created:  2021-05-25
4
* Last modified: 2021-07-20
5
* Version:       1.11
6
* Project:       ForwardCom library libc_light.li
7
* Description:   gets_s: Read a string from standard input until newline
8
* This version is for small CPUs with limited capabilities
9
* The following instructions are avoided: mul, div, push, pop, sys_call.
10
* Input comes directly from input port 8
11
* Output to stdout goes directly to output port 10
12
*
13
* C declaration:
14
* char *gets_s (char *string, size_t max_length);
15
*
16
* This function is defined in the C11 standard
17
* It will read a string from stdin until a newline or carriage return is
18
* encountered or the maximum length is reached.
19
* The input is echoed to standard output.
20
* The newline or carriage return is not included in the string.
21
* A terminating zero is always inserted at the end of the string.
22
* If the string length, including the terminating zero, exceeds max_length
23
* then the superfluous characters are discarded and it keeps reading until
24
* a newline or carriage return.
25
* This version does not raise any exception in case max_length is exceeded,
26
* but returns a truncated string.
27
* A NULL pointer is returned if any of the input parameters are zero.
28
* The C standard says that reading ends at newline, but Windows versions end
29
* at carriage return. This version supports both for the sake of compatibility.
30
*
31
* Copyright 2021 GNU General Public License http://www.gnu.org/licenses
32
*****************************************************************************/
33
 
34
// define input and output port numbers
35
%stdin_port = 8
36
%stdin_status_port = 9
37
%stdout_port = 10
38
 
39
 
40
code section execute
41
 
42
_gets_s function public reguse = 3, 0
43
 
44
  // save r2, r3
45
  int64 sp -= 2*8
46
  int64 [sp] = r2
47
  int64 [sp+8] = r3
48
 
49
  // check if parameters are valid
50
  if (int64 r0 == 0) {jump gets_s_error}         // error: null pointer or zero length
51
  if (int64 r1 == 0) {jump gets_s_error}         // error: zero length
52
 
53
  int64 r2 = r0                                  // point to current position in output buffer
54
 
55
  // loop until newline or maximum string length
56
  while (true) {
57
 
58
    // wait for input
59
    do {
60
      int16 r3 = input(r3, stdin_status_port)    // check if there is input
61
    }
62
    while (int r3 == 0);
63
 
64
    int8 r3 = input(r3, stdin_port)              // read one byte from input
65
    int8 output(r3, r3, stdout_port)             // echo input
66
    if (int r3 == 10) {break}                    // stop at newline
67
    //if (int r3 == 13) {break}                  // optionally stop at charriage return
68
    int64 r1--                                   // count down maximum length
69
    if (int64 r1 <= 0) {continue}                // length limit reached. keep reading until newline
70
    int8 [r2] = r3                               // store byte as character in output buffer
71
    int64 r2++                                   // increment string pointer
72
  }
73
  int  r3 = 0                                    // insert terminating zero
74
  int8 [r2] = r3
75
  gets_s_9:
76
 
77
  // restore r2, r3
78
  int64 r2 = [sp]
79
  int64 r3 = [sp+8]
80
  int64 sp += 2*8
81
  return
82
 
83
  gets_s_error:
84
  int r0 = 0
85
  jump gets_s_9
86
 
87
code end

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.