From Newsgroup: alt.lang.asm
ch7u3.mac
ch7u4.mac
The first program emulates MID$ (from BASIC). TOPS-20 has a BASIC so I
could see how it worked. The second program reads from the terminal and
stores strings. Then you read from that memory and convert to integers.
@exec ch7u3
LINK: Loading
[LNKXCT CH7U3 execution]
Enter a short string to demo MID$: Where is Paris?
Enter an integer position to begin the substring: 10
Enter an integer reflecting the substring length: 5
Paris
@cont
Enter a short string to demo MID$: Hello world
Enter an integer position to begin the substring: 0
Invalid start position
comment %
Write a program that simulates a MID$ (in BASIC) function. The program
should accept a string and two numbers and then print the partial string.
Ex:
@basic
READY
10 a$ = "Where is Paris?"
20 print mid$( a$, 10, 5 )
run
NONAME.B20
Thursday, June 25, 2026 11:54:54
Paris
%
title ch7u3
search monsym
ac1=1 ; Label used accumulators
ac2=2
ac3=3
count=4 ; How many chars in substring?
size==36 ; 30 dec, don't show sym in debugger buffer: block size ; 30 words * 5 = 150 ASCII chars ptrbuf: point 7,buffer ; Pointer to input buffer
base10: 12 ; 10 decimal, base we in/output with pos: 0 ; Starting postion of substring
ttyout: .priou ; Display device
ttyin: .priin ; Keyboard input device
strmsg: asciz /Enter a short string to demo MID$: /
inimsg: asciz /Enter an integer position to begin the substring: /
endmsg: asciz /Enter an integer reflecting the substring length: /
outmsg: asciz /
The substring is / ; User prompts and messages
subttl input
start: hrroi ac1,strmsg ; Load user prompt
psout% ; Display
move ac1,ptrbuf ; Set up buffer for RDTTY%
movei ac2,size-1 ; Num chars to read, allow for null end
setz ac3, ; No ctrl-r, not needed on modern TTY
rdtty% ; Read the user's test string
erjmp error ; Handle error
getn1: hrroi ac1,inimsg ; Load prompt to get start pos
psout% ; Display it
; nin% wants device, radix to 1, 3 and puts results in ac2
move ac1,ttyin ; Ready NIN%
move ac3,base10 ; Input base is decimal
nin% ; Get starting pos number
erjmp error ; Handle error
caig ac2,0 ; Don't allow zero or less
jrst [move ac1,[point 7,[asciz /Invalid start position/]]
psout%
jumpa done] ; Exit w/error if not > 1
subi ac2,1 ; Adjust start position from zero
; indexing to one, which is what
; a user would expect
movem ac2,pos ; Save starting position
getn2: hrroi ac1,endmsg ; Load prompt to get num chars
psout% ; Display it
move ac1,ttyin ; Read from TTY
move ac3,base10 ; Still using decimal
nin% ; Get num chars to read
erjmp error ; Handle error
move count,ac2 ; Save num of chars in substring
subttl substring
subs.1: move ac2,ptrbuf ; Load pointer to char buffer
move ac3,pos ; Fetch starting substring position
adjbp ac3,ac2 ; Move pointer to new starting pos
movem ac3,ac2 ; Save result into memory
subs.2: ildb ac1,ac3 ; Load next byte in ptr ac3 for ac1
cain ac1,15 ; \r? If so, we're end-of-string
jrst done ; with nothing more to print, exit
pbout% ; Print ac1's character
sojg count,subs.2 ; Loop if still chars (count)
done: haltf% ; Exit to monitor
jrst start ; Restartable program
; The error message is very generic because this is only an exercise
error: hrroi ac1,[point 7,[asciz /Error, can't demo MID$/ ] ]
haltf% ; Exit to monitor
lit ; Display literals on debug
end start ; Assembler's job is done
This program was harder because the text doesn't spend much time
explaining using NIN% pointed at memory. It gets numbers as strings,
then ORs them and outputs them as numbers. Think atoi() in C. This
program also uses a reprompter (ctrl-r), though outside a paper TTY it's
not that useful.
@exec ch7u4
LINK: Loading
[LNKXCT CH7U4 execution]
Enter first 5-bit binary number: 00101
Enter second 5-bit binary number: 01010
Final result: 01111
comment $
Set up an RDTTY%/NIN% to accept two 5-bit binary numbers. IOR the
numbers; then NOUT% the result in a field of width 5, with leading
zeroes.
Additional info on using RDTTY%/NIN% in conjunction:
https://www.bourguet.org/v2/pdp10/jsys-user/chap2 , section 2.9
$
title ch7u4
search monsym
ac1=1 ; Label used accumulators
ac2=2
ac3=3
ac4=4
num1: block 2 ; 2 * 5 = 10 chars space
num2: block 2 ; 2 * 5 = 10 chars space
ptrnm1: point 7,num1 ; Pointer to first buffer
ptrnm2: point 7,num2 ; Pointer to second buff
msg1: asciz /Enter first 5-bit binary number: /
msg2: asciz /Enter second 5-bit binary number: /
finmsg: asciz /Final result: / ; User messages and prompts
start: hrroi ac1,msg1 ; Prompt for first number
psout% ; Display message to TTY
move ac1,ptrnm1 ; Set up buffer for RDTTY%
movei ac2,5+2 ; 5 chars plus \r\n
hrroi ac3,[asciz /First number? /] ; Reprompter message
rdtty% ; Get first number
erjmp [move ac1, [point 7, [asciz /Error getting first number /]]
psout%
haltf%] ; Handle error here
hrroi ac1,msg2 ; Prompt for second number
psout% ; Print to TTY
move ac1,ptrnm2 ; Set up buffer for RDTTY%
movei ac2,5+2 ; 5 chars plus \r\n
hrroi ac3,[asciz /Second number? /]
rdtty% ; Get second number
erjmp [move ac1, [point 7, [asciz /Error getting num 2 /]]
psout%
haltf%] ; Handle error on num2
hrroi ac1,finmsg ; Load results message
psout% ; Print to TTY
; This section uses NIN% to read the inputted data from memory and
; convert it. In this case, give ac1 a pointer to the buffer.
; nin% wants device, radix to 1, 3 and puts results in ac2
conv: move ac1,ptrnm1 ; Load pointer to buffer, num1
movei ac3,2 ; Convert to base binary
nin% ; Fetch number
erjmp [move ac1, [point 7, [asciz /Conversion error/ ]]
psout%
haltf%] ; Handle conv error
move ac4,ac2 ; Park this for a moment
move ac1,ptrnm2 ; Load pointer to buff/num1
movei ac3,2 ; Convert to base binary
nin% ; Fetch second number
erjmp [move ac1, [point 7, [asciz /Conversion error/ ]]
psout%
haltf%] ; Handle 2nd conv error
or: ior ac4,ac2 ; Perform the IOR, result in ac4
; nout% wants device, number, base in accum 1,2,3 and erjmp
move ac1,[.priou] ; Indicate TTY as output
move ac2,ac4 ; The resulting number
movei ac3,2 ; Indicate binary base output
; The left half of ac3 controls the display of the number with NOUT%
; Each bit sets a feature. Note "NO%*" type values do not seem to work
; even though the text mentions them. Use a binary string instead.
hrli ac3,^B1110000000000101 ; Leading 0's, 5 places
nout% ; Print resulting number
erjmp done ; Handle error (or not)
done: haltf% ; Exit to monitor
jrst start ; Restart program
lit ; Debugger expands literals
end start ; Assembler is done
--
PGP Key ID: 781C A3E2 C6ED 70A6 B356 7AF5 B510 542E D460 5CAE
"The Internet should always be the Wild West!"
--- Synchronet 3.22a-Linux NewsLink 1.2