LC-3 - How to store input character then print input character with both input and output after strings? - lc3

I am trying to input a single character on the same line as a string and then output that character on a line with a string as well. I have tried GETC and PUTC but I am getting the result of '0 I am really new to this LC-3 stuff and would really appreciate some help to get over this bump in the road.
Here is what I have so far.
.ORIG x3000 ;start assembly directive
MyMain
lea r0, input ;point to input string
trap x22 ;print string out
GETC
ld r0, newLine ;get <crlf>
trap x21 ;print it out
lea r0, output ;point to output string
trap x22 ;print string out
PUTC
ld r0, newLine ;get <crlf>
trap x21 ;print it out
lea r0, term ;point to termination string
trap x22 ;print string out
ld r0, newLine ;get <crlf>
trap x21 ;print it out
MyMainEnd trap x25 ;stop the program
; constants
newLine .FILL x0A ;line feed and Carriage return in LC-3
input .STRINGZ "Please input a character: "
output .STRINGZ "You input the character: "
term .STRINGZ "Program execution terminated!"
.END ;end assembly directive

Here is the documentation for GETC
GETC - Read a single character from the keyboard. The character is not echoed onto the console. Its ASCII code is copied into R0. The high 8 bits of R0 are cleared
Your issue is using R0 for everything as the ld r0, newline will clobber the character you read in. After you call the GETC trap you will need to copy R0's value into some other register and then move it back to R0 when you want to call PUTC.
Also from your question you will need to call PUTC twice. Immediately after your GETC and then after you output the new line character.

Related

Bash. cat file interpret text as usual, but cat -t text find some symvals (^A) etc

What is that symbol meaning?
cat text.txt
str !
()
()
()
end.
cat -t text.txt
str !
(^A)
(^B)
(^F)
^I end.
what that symbol meaning? Why does it view with flag t?
what that symbol meaning?
From https://en.wikipedia.org/wiki/Caret_notation :
Caret notation is a notation for control characters in ASCII. The notation assigns ^A to control-code 1, sequentially through the alphabet to ^Z assigned to control-code 26 (0x1A). For the control-codes outside of the range 1–26, the notation extends to the adjacent, non-alphabetic ASCII characters.
^A means byte 0x01, ^B means byte 0x02, etc.

Shell: how to pass georgian (utf-8) character key to a specific field on a website with lynx

lynx -accept_all_cookies $URL -cmd_script=bar.txt
bar.txt:
key <tab> //get to first field
key <tab> //get to second field
key ძ //utf=8 input ძ
key ე //utf=8 input ე
key ბ //utf=8 input ბ
key ნ //utf=8 input ნ
key ა //utf=8 input ა
key <tab> //get to third field
key <tab> //get to fourth field
key <tab> //get to sumbit button
key ^J //click submit and wait for load
key <tab> //get to hyperlink
key ^J //click hyperlink and wait for load
key Q //exit
key y //confirm exit
The above attempt, adapted from this SO question, works fine for Ascii characters but not for Georgian input characters.
Any suggestions?
The characters you want to send are multibyte UTF-8 sequences, and the lynx command script key command only sends a single 8-bit byte. So you have to break the characters into individual bytes and send each byte separately.
That's a little annoying; the simplest way to do it might be to use the -cmd_log option to create a log file while you type the characters you want to send.
However, you can do this with bash:
$ georgian=ძებნა
$ (export LC_ALL=C; printf "key 0x%2x\n" $(sed 's/./"& /g' <<<"$georgian"); )
key 0xe1
key 0x83
key 0xab
key 0xe1
key 0x83
key 0x94
key 0xe1
key 0x83
key 0x91
key 0xe1
key 0x83
key 0x9c
key 0xe1
key 0x83
key 0x90
That bash command is probably a little obscure :-). Here's a quick breakdown:
( # Start a subshell to isolate environment change
export LC_ALL=C; # In the subshell, set the locale to plain ascii
printf "key 0x%2x\n" # Apply this format to each argument
$( # Substitute the result of running the following
sed # Editor command
's/./"& /g' # Change each byte to <"><byte><space>
<<<"$georgian" # Fabricate an input stream for sed
# using the value of shell variable $georgian
); # End of command substitution
) # End of subshell
Unlike C printf (and others), the shell printf repeats its pattern until all arguments have been handled. And, as another idiosyncracy, if the format is numeric (such as %x, which formats its argument in hexadecimal and the corresponding argument starts with a ", the number used for the argument is the character code of the second character in the argument (i.e. the one after the ").
The sed command turns a byte-sequence into a series of arguments with exactly this form; it's important to not quote the command substitution so that the individual substitutions will be treated as separate arguments. That means that the command won't work if $georgian contains any shell metacharacter, but since all shell metacharacters are simple ascii characters, there won't be any problem provided that every character in $georgian is, in fact, Georgian. (But don't put spaces in the string. They'll get silently dropped.)
Here is my alternative for breaking a multi-byte character into individual bytes:
$ georgian=ძებნა
$ echo -n "$georgian" | od -w1 -An -t xC | sed 's|^ |key 0x|'
key 0xe1
key 0x83
key 0xab
key 0xe1
key 0x83
key 0x94
key 0xe1
key 0x83
key 0x91
key 0xe1
key 0x83
key 0x9c
key 0xe1
key 0x83
key 0x90

How to delete lines from pattern_1 to the second occurence of pattern_2 with sed?

i need to find a command to delete lines from pattern_1 to the second occurence of pattern_2 (pattern_1 and second occurence of pattern_2 included) with sed.
random_line_1
pattern_1
pattern_2
random_line_3
random_line_4
random_line_5
pattern_2
random_line_6
i need to obtain :
random_line_1
random_line_6
I tried lots of commands inspired by what i have found everywhere but nothing works...
any idea?
Would you please try the following:
sed -n '
/pattern_1/ { ;# if the line matches "pattern_1"
:l1 ;# then enter a loop for "pattern_2"
n
/pattern_2/ { ;# if the line matches the 1st "pattern_2"
:l2 ;# then enter an inner loop for next "pattern_2"
n
/pattern_2/ { ;# if the line matches the 2nd "pattern_2"
b ;# then exit the loop
}
b l2 ;# else jump to "l2"
}
b l1
}
p ;# print the pattern space
' file

find text between 2 strings if not found blank

I am trying to get string between device_uuid: and ,
d
device_uuid:xxx,
ptr
device_uuid:2,
command:
sed -e 's/device_uuid:\(.*\),/\1/g' d
Output :
xxx
ptr
2
Expected output:
xxx
== > blank as there is no pattern
2
Need some more advanced sed commands here:
sed 's/device_uuid:\([^,]*\),/\1/; tEnd; s/.*//; :End' <<DATA
device_uuid:xxx,
ptr
device_uuid:2,
DATA
xxx
2
The t command jumps to a label if the previous s command made a substitution, and the : command defined the label.
https://www.gnu.org/software/sed/manual/sed.html#Programming-Commands
May be easier to read with newlines instead of semicolons
sed '
s/device_uuid:\([^,]*\),/\1/
tEnd
s/.*//
:End
'

How do to replace ^A characters with tr

I want to replace ^A characters with \n with tr.
I dont have a clue what ^A represents.
What ASCII code shall I use for ^A ?
^A means ASCII 0x01 (SOH). You can remove it with tr -d '\001'.
The ASCII control characters are often represented in this way, with ^X having the ASCII code of X minus 0x40.
ASCII 0 (NUL) is ^#
ASCII 1 (SOH) is ^A
ASCII 2 (STX) is ^B
...
You can produce them by pressing Ctrl-#, Ctrl-A, Ctrl-B, ...

Resources