EE 306 - Code from Lecture
Fall 2004
Encryption - 10/11/04
.asm file .bin file
This program encrypts 10 characters typed from the keyboard. Encrypted characters
will be stored in memory and displayed on the screen.
Encryption Algorithm: Add 10 to the 1st character, 9 to the 2nd character,...1 to
the 10th character.
.ORIG x3000
LEA R1, BUFFER ; R1 will be pointer to buffer
LD R2, TEN ; R2 will contain k, the encryption character
NEXTIN BRnz OUTPUT
TRAP x23 ; get a character to encrypt
ADD R0, R0, R2 ; encrypt
STR R0, R1, #0
ADD R1, R1, #1 ; increment pointer
ADD R2, R2, #-1 ; decrement k
BRnzp NEXTIN
OUTPUT LEA R1, BUFFER
LD R2, TEN
NEXTOUT BRnz DONE
LDR R0, R1, #0
TRAP x21 ; display character
ADD R1, R1, #1
ADD R2, R2, #-1
BRnzp NEXTOUT
DONE TRAP x25
BUFFER .BLKW #10
TEN .FILL #10
.END
Following is the machine code that the assembler would generate for the assembly
language program above. The first column of numbers shows which memory location
each instruction would be stored in. NOTE: Locations x3012 to
x301B will be set aside by the .BLKW pseudo-op, but we have no idea what the
contents of those locations will be.
0011 0000 0000 0000
x3000 1110 001 000010001
x3001 0010 001 000011010
x3002 0000 110 000000110
x3003 1111 0000 0010 0011
x3004 0001 000 000 000 010
x3005 0111 000 001 000000
x3006 0001 001 001 1 00001
x3007 0001 010 010 1 11111
x3008 0000 111 111111001
x3009 1110 001 000001000
x300A 0010 010 000010001
x300B 0000 110 000000101
x300C 0110 000 001 000000
x300D 1111 0000 0010 0001
x300E 0001 001 001 1 00001
x300F 0001 010 010 1 11111
x3010 0000 111 111111010
x3011 1111 0000 0010 0101
x3012 garbage
x3013 garbage
x3014 garbage
x3015 garbage
x3016 garbage
x3017 garbage
x3018 garbage
x3019 garbage
x301A garbage
x301B garbage
x301C 0000 0000 0000 1010
Character Count - Updated to include code from 10/4/04
The starting address of the file is x4000. This program assumes that the count will be less than 10.
NOTE: The opcode for the third instruction (AND) was incorrectly written as 0001
in class. The opcode should be 0101 (as shown below).
x3000 1111 0000 0010 0011 ; get character from user (will be put in R0)
x3001 0010 001 000010001 ; R1<-starting address of file (R1 will be pointer to file)
x3002 0101 010 010 1 00000 ; R2<-0 (R2 will be used for count)
x3003 0110 011 001 000000 ; get first character in file pointed to by R1
x3004 0001 100 011 1 11100 ; check for EOT character (x04)
x3005 0000 010 000001000 ; branch to x300E if it was the EOT
x3006 1001 011 011 111111 ; form the 2's complement of the character from the file
x3007 0001 011 011 1 00001
x3008 0001 100 011 000 000 ; compare the character from the file and the typed character
x3009 0000 101 000000001 ; branch to x300B if they aren't equal
x300A 0001 010 010 1 00001 ; characters matched - increment count
x300B 0001 001 001 1 00001 ; increment pointer to point to next character in file
x300C 0110 011 001 000000 ; get next character from file
x300D 0000 111 111110110 ; branch back to x3004 to check for EOT
x300E 0010 000 000000011 ; R0<-ASCII mask (x30)
x300F 0001 000 000 000 010 ; add ASCII mask to count
x3010 1111 0000 0010 0001 ; output count to monitor
x3011 1111 0000 0010 0101 ; HALT the computer (i.e., we're done!)
x3012 0000 0000 0011 0000 ; ASCII mask (x30)
x3013 0100 0000 0000 0000 ; address of first character in the file