LC3 trap executed illegal vector number - lc3

I'm trying to count the number of characters in LC3 simulator and keep getting "a trap was executed with an illegal vector number".
These are the objects I execute
charcount.obj:
0011000000000000
0101010010100000
0010011000010000
1111000000100011
0110001011000000
0001100001111100
0000010000001000
1001001001111111
0001001001100001
0001001001000000
0000101000000001
0001010010100001
0001011011100001
0110001011000000
0000111111110110
0010000000000100
0001000000000010
1111000000100001
1111000000100101
1110001011111111
0000000000110000
and verse:
.ORIG x3100
.STRINGZ "Simple Simon met a pieman,"
.STRINGZ "Going to the fair;"
.STRINGZ "Says Simple Simon to the pieman,"
.STRINGZ "Let me taste your ware."
.FILL x04
.END

Looks like we're going to need more information before we can help you much. I understand you've provided us with some binary and I ran that through the LC3 simulator. Here's where I'm a bit lost, which string would you like to count and where is it stored?
After trying to piece together what you've provided here's what I've found.
Registers:
R0 x0061 97
R1 x0000 0
R2 x0000 0
R3 xE2FF -7425
R4 x0000 0
R5 x0000 0
R6 x0000 0
R7 x3003 12291
PC x3004 12292
IR x62C0 25280
CC Z
Memory:
x3000 0101010010100000 x54A0 AND R2, R2, #0
x3001 0010011000010000 x2610 LD R3, x3012
x3002 1111000000100011 xF023 TRAP IN
x3003 0110001011000000 x62C0 LDR R1, R3, #0
x3004 0001100001111100 x187C ADD R4, R1, #-4
x3005 0000010000001000 x0408 BRZ x300E
x3006 1001001001111111 x927F NOT R1, R1
x3007 0001001001100001 x1261 ADD R1, R1, #1
x3008 0001001001000000 x1240 ADD R1, R1, R0
x3009 0000101000000001 x0A01 BRNP x300B
x300A 0001010010100001 x14A1 ADD R2, R2, #1
x300B 0001011011100001 x16E1 ADD R3, R3, #1
x300C 0110001011000000 x62C0 LDR R1, R3, #0
x300D 0000111111110110 x0FF6 BRNZP x3004
x300E 0010000000000100 x2004 LD R0, x3013
x300F 0001000000000010 x1002 ADD R0, R0, R2
x3010 1111000000100001 xF021 TRAP OUT
x3011 1111000000100101 xF025 TRAP HALT
x3012 1110001011111111 xE2FF LEA R1, x3112
x3013 0000000000110000 x0030 NOP
The values displayed in the registers is what I get when I stop after line x3003. For some reason the literal value of xE2FF gets loaded into register R3. After that the value of 0 at memory location xE2FF is loaded into register R1 and then the problems mount from there.
I would recommend displaying your asm code and then commenting each line so we can better understand what you're trying to accomplish.

Related

I can't find out why the loop isn't stopping-LC3

I'm just a super newbie like I just learn how to do this just for 12 hrs I was wondering why my loop is not stopping. Can you help me find what is wrong. I know this code is garbage, please bear with me.
So our task is to ask the user to ask the user input a string with max 80 characters and should end with period since it is our basis to know if it is the end of the string. The program will count the characters and words and display it, but in my case the program doesn't stop. Please help.
.ORIG X3000
LEA R0, PROMPT_ENTER ;Message for entering number.
PUTS
LEA R2, SENTENCE ;allocated memory
AND R3, R3, #0 ;setting R3 to zero for word counter.
ADD R3, R3, #1
AND R1, R1, #0 ;setting R4 to zero for char counter.
;---------ASKING USER TO INPUT A SENTENCE------
GET_USER_INPUT: ;loop for getting characters.
GETC
OUT
STR R0, R2, #0 ;r0 -> ( memory address stored in r2 + 0 )
PUT
ADD R2, R2, #1 ;increments the memory pointer
ADD R0, R0, #-10 ;decrements loop to proceed when pressed enter.
BRz COUNT_LENGTH
BRnp GET_USER_INPUT
;--------Element counter----
COUNT_LENGTH:
AND R0, R0, #0
LEA R4, SENTENCE
LDR R0, R4, #0
ADD R0, R0, #-10
BRz EMPTY
BRnp COUNT_ELEMENTS
EMPTY:
AND R0, R0, #0
LEA R0, PROMPT_NULL
PUTS
HALT
COUNT_ELEMENTS:
AND R0, R0, #0
LEA R4, SENTENCE
LDR R0, R4, #0
LD R6, TMNT
ADD R0, R0, R6
BRz END_OF_SENTENCE
LDR R0, R4, #0
LD R6, SPACE
ADD R0, R0, R6
BRz WORD_COUNT
ADD R4, R4, #1
ADD R1, R1, #1
BRnp COUNT_ELEMENTS
WORD_COUNT:
ADD R4, R4, #1
ADD R3, R3, #1
JSR COUNT_ELEMENTS
END_OF_SENTENCE:
AND R0, R0, #0
LDR R3, R3, #0
LD R5, ASCII
ADD R0, R0, R5
OUT
AND R0, R0, #0
LDR R1, R1, #0
ADD R0, R0, R1
OUT
HALT
SENTENCE .BLKW #80 ;initialize the array named sentence with length 80
TMNT .fill #-89
SPACE .fill #-32
ASCII .fill #48
;----MESSAGES------
PROMPT_ENTER .stringz "Enter the word(maximum 80 characters): \n"
PROMPT_AGAIN .stringz "Do you want to try again? Y/N: \n"
PROMPT_NULL .stringz "Error: Please enter a sentence!"
PROMPT_NOTMNT .stringz "Error: No terminating symbol (.) is expected at the end!"
PROMPT_DSPACE .stringz "Error: Multiple white space is not allowed!"
.END
I've only skimmed though this code.
it is an infinite loop because you reset R4 to point to the start of SENTENCE in each iteration of COUNT_ELEMENTS.
I can see in your code where you are incrementing R4 before going back to COUNT_ELEMENTS (btw JSR is only used to call a subroutine if you want to Branch unconditionally use BR).
You'd want to set R4 to point to SENTENCE only once. I do believe you can simply remove the LEA R4, SENTENCE within COUNT_ELEMENTS since it was set previously as part of COUNT_LENGTH.
In the future I would recommend pulling your code up in a lc3 simulator and stepping through it examining the values of the registers as you step though.

"Access to unaligned memory location, bad address=ffffff"

I'm trying to read integers from an input.txt file, below is my read loop where I'm attempting to read and store the integers into an array. I keep getting "Access to unaligned memory location, bad address=ffffff" on any line after the line with "LDR R2, [R2,R5,LSL #2]...im using ARM SIM. Does anyone know what I'm doing wrong?
start:
MOV R5, #0 #int i
MOV R1, #0
swi SWI_Open
LDR R1,=InFileH
STR R0,[R1]
MOV R3, #0
readloop:
LDR R0, =InFileH
LDR R0, [R0]
swi SWI_RdInt
CMP R0, #0
BEQ readdone
#the int is now in R0
MOV R1, R0
LDR R3,=a
STR R2,[R3,R5,LSR#2]
MOV R2, R1
ADD R5, R5, #1 #i++
bal readloop
readdone:
MOV R0, #0
swi SWI_Close
swi SWI_Exit
.data
.align 4
InFileH: .skip 4
InFile: .asciz "numbers.txt"
OutFile: .asciz "numsort.txt"
OutFileH: .skip 4
NewLine: .asciz "\n"
a: .skip 400
i had faced similar issue while programming arm assembly
this was because it was expecting offset in multiples of 4
STR R2, [R1, #2]
the above instruction throws the similar error. so it was resolved by using
STR R2, [R1, #4]
for better understanding clickhere

LC3 continue getting a trap was executed with an illegal vector number

I am trying to create a program that will present the number input in binary to the user. Currently, all I have is the setup to get the user's number once they are finished typing all of their characters, however I don't understand why the code below will not run.
.ORIG x3000
RESET
AND R1, R1, #0
AND R2, R2, #0
AND R3, R3, #0
AND R4, R4, #0
ASCII .FILL #-48 ;ASCII CONVERSION
LD R5, ASCII ;
AND R6, R6, #0 ;NEGATIVE FLAG
DISPLAY .STRINGZ "\nTYPE A NUMBER THEN PRESS ENTER: "
LEA R0 DISPLAY
PUTS
loop
LOOP
GETC
OUT
AND R4, R4, #0 ;CHECK IF LF
ADD R4, R4, #-10 ;
ADD R4, R4, R0 ;
BRZ READY
LD R4, CHECKN ;check if negative
AND R4, R4, #0 ;
ADD R4, R4, R0 ;
BRZ NEGATIVE ;
ADD R1, R0, R5
BRNZP MULTIPLY
ADD R2, R1, R3
BRNZP LOOP
NEGATIVE
ADD R6, R6, #1
BRNZP LOOP
multiply by adding the same number 10 times
MULTIPLY
ADD R3, R2, R2
ADD R3, R3, R2
ADD R3, R3, R2
ADD R3, R3, R2
ADD R3, R3, R2
ADD R3, R3, R2
ADD R3, R3, R2
ADD R3, R3, R2
ADD R3, R3, R2
BRNZP LOOP
CHECKN .FILL #-45
READY
HALT
.END

Introductory ARM - Assembly Error

For a class just starting out with the ARM assembly language, we are required to implement a simple for-loop described below:
h=1;
for (i=0, i<5, i++)
h=(h*3)-i;
I have written the following code in ARM assembly:
AREA Prog2, CODE, READONLY
ENTRY
MOV r0, #1; initialize h=1
MOV r1, #0; initialize i=0
loop CMP r1, #5; at start of loop, compare i with 5
MULLT r0, r0, #3; if i<5, h=h*3
SUBLT r0, r0, r1; if i<5, h=h-i (ties in with previous line)
ADDLT r1, r1, #1; increment i if i is less than 5
BLT loop ; repeat loop of i is less than 5
stop B stop; stop program
END
The problem is that there is an error with the line
MULLT r0, r0, #3; if i<5, h=h*3
If I delete it from the code, everything works fine. I just cannot understand the issue with this one line. The error description given is "Bad register name symbol, expected integer register." I have tried loading #3 into a register then multiplying the two registers, but that didn't help. It simply changed the error message to "This register combination results in unpredictable behavior." I am new to this, so please offer only basic instructions as a fix for this. Thanks.
MUL requires all operands to be registers, so you must use the form MUL r0, rn, r0 where rn is some other suitable register.
If the result and the first operand are the same the result is unpredictable as the error says. This is due to the internal operation of the processor. This is why you must use r0, rn, r0 and not r0, r0, rn
Multiplying by 3 is overrated anyway; ARM can do it with a single ridiculously idiomatic addition:
add r0, r0, r0 lsl #1 // r0 = r0 + r0 *2

ARM Assembly: Absolute Value Function: Are two or three lines faster?

In my embedded systems class, we were asked to re-code the given C-function AbsVal into ARM Assembly.
We were told that the best we could do was 3-lines. I was determined to find a 2-line solution and eventually did, but the question I have now is whether I actually decreased performance or increased it.
The C-code:
unsigned long absval(signed long x){
unsigned long int signext;
signext = (x >= 0) ? 0 : -1; //This can be done with an ASR instruction
return (x + signet) ^ signext;
}
The TA/Professor's 3-line solution
ASR R1, R0, #31 ; R1 <- (x >= 0) ? 0 : -1
ADD R0, R0, R1 ; R0 <- R0 + R1
EOR R0, R0, R1 ; R0 <- R0 ^ R1
My 2-line solution
ADD R1, R0, R0, ASR #31 ; R1 <- x + (x >= 0) ? 0 : -1
EOR R0, R1, R0, ASR #31 ; R0 <- R1 ^ (x >= 0) ? 0 : -1
There are a couple of places I can see potential performance differences:
The addition of one extra Arithmetic Shift Right call
The removal of one memory fetch
So, which one is actually faster? Does it depend upon the processor or memory access speed?
Here is a nother two instruction version:
cmp r0, #0
rsblt r0, r0, #0
Which translate to the simple code:
if (r0 < 0)
{
r0 = 0-r0;
}
That code should be pretty fast, even on modern ARM-CPU cores like the Cortex-A8 and A9.
Dive over to ARM.com and grab the Cortex-M3 datasheet. Section 3.3.1 on page 3-4 has the instruction timings. Fortunately they're quite straightforward on the Cortex-M3.
We can see from those timings that in a perfect 'no wait state' system your professor's example takes 3 cycles:
ASR R1, R0, #31 ; 1 cycle
ADD R0, R0, R1 ; 1 cycle
EOR R0, R0, R1 ; 1 cycle
; total: 3 cycles
and your version takes two cycles:
ADD R1, R0, R0, ASR #31 ; 1 cycle
EOR R0, R1, R0, ASR #31 ; 1 cycle
; total: 2 cycles
So yours is, theoretically, faster.
You mention "The removal of one memory fetch", but is that true? How big are the respective routines? Since we're dealing with Thumb-2 we have a mix of 16-bit and 32-bit instructions available. Let's see how they assemble:
Their version (adjusted for UAL syntax):
.syntax unified
.text
.thumb
abs:
asrs r1, r0, #31
adds r0, r0, r1
eors r0, r0, r1
Assembles to:
00000000 17c1 asrs r1, r0, #31
00000002 1840 adds r0, r0, r1
00000004 4048 eors r0, r1
That's 3x2 = 6 bytes.
Your version (again, adjusted for UAL syntax):
.syntax unified
.text
.thumb
abs:
add.w r1, r0, r0, asr #31
eor.w r0, r1, r0, asr #31
Assembles to:
00000000 eb0071e0 add.w r1, r0, r0, asr #31
00000004 ea8170e0 eor.w r0, r1, r0, asr #31
That's 2x4 = 8 bytes.
So instead of removing a memory fetch you've actually increased the size of the code.
But does this affect performance? My advice would be to benchmark.

Resources