Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Describe what the following lC-3 program does in a few sentences. .ORIG x3000 LE

ID: 3707359 • Letter: D

Question

Describe what the following lC-3 program does in a few sentences.

.ORIG x3000

        LEA R1, STRZ

        AND R2, R2, #0

        LD R4, CHAR

REPEAT LDR R3, R1, #0    

        BRz FINISH                    ;Branch if null

        ADD R3, R3, R4

        BRnp PASS                    ;Branch if not ' '

        ADD R2, R2, #1

PASS    ADD R1, R1, #1

        BR REPEAT                  ;Always branch

FINISH ST R2, COUNT

        HALT

CHAR    .FILL xFFE0

COUNT   .FILL x0000

STRZ    .STRINGZ "This is CS252!"

.END

Explanation / Answer

.ORIG x3000

LEA R1, STRZ

AND R2, R2, #0

LD R4, CHAR

REPEAT LDR R3, R1, #0   

BRz FINISH ;Branch if null

ADD R3, R3, R4

BRnp PASS ;Branch if not ' '

ADD R2, R2, #1

PASS ADD R1, R1, #1

BR REPEAT ;Always branch

FINISH ST R2, COUNT

HALT

CHAR .FILL xFFE0

COUNT .FILL x0000

STRZ .STRINGZ "This is CS252!"

.END
//////////

Below is some breif explanation about above program.

REPEAT LDR R3, R1, #0   

BRz FINISH ;Branch if null

ADD R3, R3, R4

BRnp PASS ;Branch if not ' '

ADD R2, R2, #1

Above will branch to REPEAT until it loads a null character in R3. The length of the string is 22 characters + 1 null character, so it gets executed 23 times.

PASS ADD R1, R1, #1

BR REPEAT ;Always branch

PASS label is executed for each character except null, so it gets executed 22 times


Above program counts the number of space character in the string at STRZ(This is CS252!) and stores that value in COUNT.