Reputation: 47
Hey Stackoverflow I was going over some example code our professor has left us to study with, and I've had some problems understanding what some of the codes mean and it doesnt help that some of his comments are vague. the first is R3,R1,0; check for end of line, the second thing I dont get is really the logic behind the beginning of the placeolnul, and lastly the .fill values for negeol which seem oddly specific but i have no idea why. if you could really help me with those issues it would really help me understand the code alot better.
.orig x3000
getstring:
lea r0,prompt ;get string prompt
puts
ld r1,negeol ;to test end of line
lea r2,rdbuff ;prep to read string
rdloop:
getc ;get string char
out
str r0,r2,0 ;store it
add r3,r1,r0 ;check for end of line
brz placeeolnul ;if so process
add r2,r2,1 ;ready for next char
br rdloop ;get it
placeeolnul:
and r0,r0,0 ;overwrite eol with
str r0,r2,0 ;nul
lea r1,rdbuff ;get address for len
jsr strlen ;get length
add r0,r0,0 ;if 0
brz quit ;then prog finished
trap xfc ;print length
lea r0,colon ;print colon
puts
lea r0,eol ;print lf
puts
br getstring ;go again
quit
halt
prompt: .stringz "Enter a string:"
eol: .fill x000d ; or x000a
.fill x0000
negeol: .fill xfff3 ; or xfff6
colon: .fill x003a
rdbuff .blkw 80
; length subroutine
strlen:
and r0,r0,0 ;counter for length
st r2,saveX2 ;save regs used
st r3,saveX3
add r2,r1,0 ;copy of string addr
cloop:
ldr r3,r2,0 ;get char
brz exit ;check for nul
add r0,r0,1 ;incr counter
add r2,r2,1 ;go to next char
br cloop ;process it
exit:
ld r2,saveX2 ;restore used regs
ld r3,saveX3
ret
saveX2: .blkw 1
saveX3: .blkw 1
.end
Upvotes: 1
Views: 1542
Reputation: 35
In regards to this:
add r3,r1,r0 ;check for end of line
Here he is setting up for the subsequent "brz" to branch on zero.
So we are adding the contents of registers r1 and r0 and putting them in r3, so that if r3 is zero, we will branch in the next line:
brz placeeolnul ;if so process
Remember that r1 is already pre-loaded with a value based on this code:
ld r1,negeol ;to test end of line
which has loaded r1 with the value at the tag negeol per this code:
negeol
.fill xfff3 ; or xfff6
So this is just a quick way to detect if an EOL character has been found by adding the compliment of the EOL character, which presumably here is xfff3.
Regarding the brz statement and the logic for it (your 2nd question):
brz placeeolnul ;if so process
As mentioned, we are branching here if the add is zero. If we do not branch (meaning we did not find a EOL character), then we will continue, but that continuation will essentially loop back to rdloop tag:
add r2,r2,#1 ;ready for next char
br rdloop ;get it
If we DO jump via the brz line, that means we got the entire string, and we are ready to process it.... so jumping to placeeolnul just puts us into the code where we can process the string:
and r0,r0,#0 ;overwrite eol with
str r0,r2,#0 ;nul
.... etc...
Not sure if you have questions with this part of the code as well.
Hope this helps.
Jeff
Upvotes: 0