3 Replies - 887 Views - Last Post: 27 October 2020 - 03:53 AM

#1 needlc3helpplz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-October 20

Look for a 5 (LC-3)

Posted 22 October 2020 - 12:50 PM

I'm trying to write a LC-3 program but im sorta a lost cause on LC-3. I can understand C, C++, Java, Python, but LC-3, nope. Just can't understand most of it no matter what. I'm hoping to get a better understanding of what I am supposed to do.

What I'm trying to do is write a program that can look through an array of positive numbers starting at x3100 and to look for the number 5 where if 5 is found, to set R0 to 1 and stop. Otherwise it continues on. Also, I want to be able to print the location of that 5, something like saying "5 was found at x31XX"

The data:

.ORIG x3100
.FILL #10
.FILL #20
.FILL #30
.FILL #40
.FILL #50
.FILL #5
.FILL #70
.FILL #5
.FILL #90
.FILL #100
.END


What I have so far

.ORIG x3000

LEA R1 ; pointer reference to x3100

AND R0, R0, #0 ; clears R0

AND R2, R2, #0 ; clears R2

LDR R2, R1, #0 ; essentially copying in the value behind R

BRz #? ; number TBD when more code is added 

.END



After that, I'm not sure much on what to do.

I'm just trying to get a better understanding on what to do.

When I used LEA, I meant to say
LEA R1, xFF


Is This A Good Question/Topic? 0
  • +

Replies To: Look for a 5 (LC-3)

#2 Ornstein   User is offline

  • D.I.C Regular

Reputation: 120
  • View blog
  • Posts: 277
  • Joined: 13-May 15

Re: Look for a 5 (LC-3)

Posted 22 October 2020 - 01:23 PM

Do you at least understand the condition codes and branching? That various instructions set the condition codes and the BR instruction checks said codes to decide how to branch, etc?

So to start with, you'll want to figure out a way of getting some instruction(s) to set one or more condition codes if/when a given value is 5. Say for example the ADD instruction will set either the zero, positive or negative codes, depending on the result of adding two numbers together; you might then try to imagine what two numbers you might be able to add together to set the zero code when a given number is 5, etc.
Was This Post Helpful? 1
  • +
  • -

#3 needlc3helpplz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-October 20

Re: Look for a 5 (LC-3)

Posted 25 October 2020 - 09:31 AM

View PostOrnstein, on 22 October 2020 - 01:23 PM, said:

Do you at least understand the condition codes and branching? That various instructions set the condition codes and the BR instruction checks said codes to decide how to branch, etc?

So to start with, you'll want to figure out a way of getting some instruction(s) to set one or more condition codes if/when a given value is 5. Say for example the ADD instruction will set either the zero, positive or negative codes, depending on the result of adding two numbers together; you might then try to imagine what two numbers you might be able to add together to set the zero code when a given number is 5, etc.

So what I did, is I set another register to -5, since the way I know if 5 was in the array or the list is by subtracting 5 because 5-5=0

So this is what I came up with so far:


.ORIG x3000

LEA R1, xFF

AND R0, R0, #0

AND R2, R2, #0

AND R3, R3, #0

AND R5, R5, #0

ADD R5, R5, #10

LDR R2, R1, #0

ADD R3, R3, #-5

AND R4, R4, #0

ADD R4, R2, R3

BRnp ELSE

ADD R0, R0, #1

TRAP x25

BRnzp AFTER

ELSE AND R4, R4, #0

AFTER ADD R0, R0, #0

BRp #4

ADD R1, R1, #1

LDR R2, R1, #0

ADD R5, R5, #-1

BRp #-12

TRAP x25

.END


So I have two further questions:

1. I'm able to get R0 to have the proper value before the TRAP x25 (HALT) executes. After that, the simulator jumps to xFD79 and the value of R0 changes. Is this something by nature that LC-3 does? As before the HALT happens, the correct values are there.

2. I'm also going to try at the extra credit of it, which is for it to print to the console that a 5 was found and print the memory location of that 5. I'm confused as towards how the structure of that would look. I'm assuming where I would add the code for it, would be the point after I set R0 to 1 and before the HALT.
Was This Post Helpful? 0
  • +
  • -

#4 Ornstein   User is offline

  • D.I.C Regular

Reputation: 120
  • View blog
  • Posts: 277
  • Joined: 13-May 15

Re: Look for a 5 (LC-3)

Posted 27 October 2020 - 03:53 AM

View Postneedlc3helpplz, on 25 October 2020 - 09:31 AM, said:

1. I'm able to get R0 to have the proper value before the TRAP x25 (HALT) executes. After that, the simulator jumps to xFD79 and the value of R0 changes. Is this something by nature that LC-3 does? As before the HALT happens, the correct values are there.


It's expected that R0 may change after the HALT, aye; control is returned to the OS (or whatever) - which may/will then start using R0 for its own purposes, etc.

View Postneedlc3helpplz, on 25 October 2020 - 09:31 AM, said:

2. I'm also going to try at the extra credit of it, which is for it to print to the console that a 5 was found and print the memory location of that 5. I'm confused as towards how the structure of that would look. I'm assuming where I would add the code for it, would be the point after I set R0 to 1 and before the HALT.


You'd want it before the HALT, yeah. I imagine you'll be wanting TRAP x21 or x22 for the output - and you'll need to figure out how to convert the memory address in the register to ASCII characters (there's a few different ways to approach that - so you may want to do some reading elsewhere and possibly come back here if you're still struggling).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1