...

View Full Version : beginning assembly language, need help



uniquity
10-20-2009, 07:02 AM
so i'm taking an intro computer organization class and we're learning assembly language right now

one of our first labs is to create a program that will ask for an encryption key and a message to be encrypted, and using an algorithm we are supposed to output the encrypted message. i have two questions concerning this.

first, the message that the user inputs is terminated with an enter, and our professor supplied us with the ascii code for enter...i coded something up so that it keeps accepting input till enter is pushed, but i think i did it wrong, or at least its not working on the lc-3 simulator :o
but heres the code i have for that section so far, can someone let me know what's wrong with it?


CHECK AND R2, R0, x000D ; check for <enter>
BRp SKIP ; if <enter>, go to SKIP
TRAP x20 ; R0 gets character input
TRAP x21 ; echo keyboard input
BRnzp CHECK ; loop back to get rest of input

my second question is, in the instructions for this program, my professor says that the program should accept ascii characters in the range x20 to x7A. looking at the examples in the textbook, i saw these things being used called ascii templates?, do those need to be used here, or does anything special have to be done to make the program only accept ascii characters in that range? i dont remember him lecturing about anything like that so i wasnt sure if i had to do anything extra for it.

thanks in advance for any input :thumbsup:

drhowarddrfine
10-20-2009, 07:41 PM
You're checking for keyboard input at the top before getting a key but that may not be an issue with this. I don't know anything about the simulator you're using, though. On the first line, you're and'ing three values. Why is that?

uniquity
10-21-2009, 03:32 AM
hehe the fact that ur asking me why i'm and'ing means my answer will probably be wrong hehe...i'm trying to compare the R0 (low end bit of the encrypted message) with x000D (enter in ascii) and putting the result in R2

drhowarddrfine
10-21-2009, 03:48 AM
Well, again, I don't know this simulator but with most processors, and'ing one register with an immediate value, 0x0d, gets the results into the same register or the memory location. There might be other processors that let the results end up in an assigned register but I haven't worked with them.

uniquity
10-21-2009, 04:11 AM
so you're saying there should only be two parameters for the AND?

like this?:

AND R0, x000D

oesxyl
10-21-2009, 04:58 AM
Well, again, I don't know this simulator but with most processors, and'ing one register with an immediate value, 0x0d, gets the results into the same register or the memory location. There might be other processors that let the results end up in an assigned register but I haven't worked with them.

http://www.scribd.com/doc/4596293/LC3-Instruction-Details


so you're saying there should only be two parameters for the AND?

like this?:

AND R0, x000D
after reading the docs, this seems to be correct:



AND R2, R0, xD


imm5 seems to be a 5 bits value. Like drhowarddrfine, I don't know the simulator and I could be wrong.

best regards

uniquity
10-21-2009, 05:19 AM
i checked the instruction set for the lc-3 simulator, and it is indeed a 5bit value

i tried doing


CHECK AND R2, R0, x0D ; check for <enter>
BRp SKIP ; if <enter>, go to SKIP


but doing that wouldnt even let me input anything into the input

i changed it to this


CHECK AND R2, R0, x0D ; check for <enter>
BRnz SKIP ; if <enter>, go to SKIP


and it allowed me to input two characters before moving on the next prompt
i think my understanding of the branch is a pretty bad, could someone clear up whats happening? hehe i get conditional statements fine in any language except for this one

oesxyl
10-21-2009, 05:37 AM
i checked the instruction set for the lc-3 simulator, and it is indeed a 5bit value

i tried doing


CHECK AND R2, R0, x0D ; check for <enter>
BRp SKIP ; if <enter>, go to SKIP


but doing that wouldnt even let me input anything into the input

i changed it to this


CHECK AND R2, R0, x0D ; check for <enter>
BRnz SKIP ; if <enter>, go to SKIP


and it allowed me to input two characters before moving on the next prompt
i think my understanding of the branch is a pretty bad, could someone clear up whats happening? hehe i get conditional statements fine in any language except for this one
do you have a label SKIP somewhere?

best regards

uniquity
10-21-2009, 06:20 AM
yes i do
i'll just post all the code i have (the programs not done yet, i havent quite finished the encrypting part or figured out the logic but heres what i got)


; Bryan Chan
; CSC 225 - Fall 2009

.ORIG x3000 ; start program at x3000
START ST R0, SaveR0 ; R0 will take in initial input
ST R3, SaveR3 ; R3 will store the input message
ST R4, SaveR4 ; R4 will store the encrypted message

;;display prompt and receive encryption key

LEA R0, Prompt1 ; load Prompt1 to R0 for output
TRAP x22 ; output R0 to screen
TRAP x20 ; R0 gets character input
ADD R1, R0, #0 ; store R0 in R1 for later
TRAP x21 ; echo keyboard input
LD R0, NewLine ; load newline to R0 for output
TRAP x21 ; output newline

;;display prompt and receive message to be encrypted

AND R0, R0, #0 ; clear R0
LEA R0, Prompt2 ; load Prompt2 to R0 for output
TRAP x22 ; output R0 to screen

;;receive message, checking for <enter> pressed

CHECK AND R2, R0, x0D ; check for <enter>
BRnz SKIP ; if <enter>, go to SKIP
TRAP x20 ; R0 gets character input
TRAP x21 ; echo keyboard input
BRnzp CHECK ; loop back to get rest of input

SKIP ADD R3, R0, #0 ; store input message to R3
LD R0, NewLine ; load newline to R0 for output
TRAP x21 ; output newline
LEA R0, Prompt3 ; load Prompt3 to R0 for output
TRAP x22 ; output R0 to screen

;;encrypt the message

CHECK2 AND R5, R3, #1 ; check for odd/even
BRp TOGGLE1 ; if low order bit is #1
ADD R3, R3, #1 ; toggle bit to #1 if #0
BRnzp NEXT ; go to NEXT

TOGGLE1 ADD R3, R3, #-1 ; toggle bit to #0 if #1

NEXT ADD R3, R3, R1 ; add R1 and R3
ADD R3, R3, R3 ; shift bit
BRnp CHECK2 ; loop back until no bits left

;;display encrypted message

ADD R0, R3, #0
TRAP x22

HALT

SaveR0 .BLKW #21
SaveR3 .BLKW #21
SaveR4 .BLKW #21
NewLine .FILL x000A
Prompt1 .STRINGZ "Encryption Key (1-9): "
Prompt2 .STRINGZ "Input Message: "
Prompt3 .STRINGZ "Encrypted Message: "
.END

oesxyl
10-21-2009, 06:51 AM
sorry but I can't follow this. I'm not familiar with that simulator, it's instruction set and is a long time since I write something in asm.
This kind of tool must have some options to put a break point somewhere in the program, to trace step by step and watch how registers and flags are changed. I think that this is all what you need. :)

best regards

uniquity
10-21-2009, 06:53 AM
hehe u probably cant follow it cause its so horribly written :( thanks for all the input anyways, greatly appreciated :thumbsup:

oesxyl
10-21-2009, 07:03 AM
hehe u probably cant follow it cause its so horribly written :( thanks for all the input anyways, greatly appreciated :thumbsup:
you are welcome and no it's not horrible, it's ok how is written, :)
from documentation I didn't understand to much and things are more clear when you can run the tool, :)

best regards

uniquity
10-21-2009, 08:26 AM
UPDATE!!! (a good one)

so i played around a bit with the code to check for <enter> and finally got it to move on to the next instruction when enter is pressed...however its still not working quite right, in particular the end, where i'm trying to do the actual encryption. the encryption algorithm is pretty simple...

it takes the ascii code of a single character from the input message
taking the low order bit of the code, it will check for a 0 or 1
then it will toggle that bit (if its a 0, turn it to a 1, vice versa)
then it will add the previously inputted encryption key (number) to the new value...i.e. if your initial input message has an ascii code of 01000111, it will become 01000110 after toggling the bit, then say the encryption key is '5', the end product for this code will be 1001011

now i have code for the encryption process, but the problem is that its not being outputted to the console, and i still dont really know how to debug my programs yet so i cant tell if the values are being encrypted correctly...the encryption process starts at psuedo-op CHECK2

here's the code i have now


.ORIG x3000 ; start program at x3000
START LD R3, SaveR3 ; R3 will store the input message
LD R4, SaveR4 ; R4 will store the encrypted message to be outputted
LD R5, Enter ; load ASCII <Enter>

;;display prompt and receive encryption key

LEA R0, Prompt1 ; load Prompt1 to R0 for output
TRAP x22 ; output R0 to screen
TRAP x20 ; R0 gets character input
ADD R1, R0, #0 ; store R0 in R1 for later
TRAP x21 ; echo keyboard input
LD R0, NewLine ; load newline to R0 for output
TRAP x21 ; output newline

;;display prompt and receive message to be encrypted

AND R0, R0, #0 ; clear R0
LEA R0, Prompt2 ; load Prompt2 to R0 for output
TRAP x22 ; output R0 to screen

;;receive message, checking for <enter> pressed

CHECK TRAP x20
TRAP x21
ADD R3, R3, R0
ADD R6, R0, R5 ; test for enter
BRz SKIP ; if enter, go to SKIP
BRnzp CHECK ; else get next character

SKIP ADD R3, R0, #0 ; store input message to R3
; LD R0, NewLine ; load newline to R0 for output
; TRAP x21 ; output newline
LEA R0, Prompt3 ; load Prompt3 to R0 for output
TRAP x22 ; output R0 to screen

;;encrypt the message

CHECK2 AND R5, R3, #1 ; check for odd/even
BRp TOGGLE1 ; if low order bit is #1
ADD R3, R3, #1 ; toggle bit to #1 if #0
BRnzp NEXT ; go to NEXT

TOGGLE1 ADD R3, R3, #-1 ; toggle bit to #0 if #1

NEXT ADD R4, R3, R1 ; add R1 and R3
ADD R3, R3, R3 ; shift bit
BRnp CHECK2 ; loop back until no bits left

;;display encrypted message

LD R0, ACSII
ADD R0, R0, R4
OUT

HALT

SaveR3 .BLKW #21 ; allocate 21 words of storage
SaveR4 .BLKW #21 ; allocate 21 words of storage
Enter .FILL xFFF6 ; ASCII code for enter '-13'
ACSII .FILL x0030 ; ASCII template
NewLine .FILL x000A ; ASCII code for a new line
Prompt1 .STRINGZ "Encryption Key (1-9): " ; first prompt
Prompt2 .STRINGZ "Input Message: " ; second prompt
Prompt3 .STRINGZ "Encrypted Message: " ; third prompt
.END

thanks in advance for any input :D

oesxyl
10-21-2009, 09:00 AM
UPDATE!!! (a good one)

so i played around a bit with the code to check for <enter> and finally got it to move on to the next instruction when enter is pressed...however its still not working quite right, in particular the end, where i'm trying to do the actual encryption. the encryption algorithm is pretty simple...

it takes the ascii code of a single character from the input message
taking the low order bit of the code, it will check for a 0 or 1
then it will toggle that bit (if its a 0, turn it to a 1, vice versa)
then it will add the previously inputted encryption key (number) to the new value...i.e. if your initial input message has an ascii code of 01000111, it will become 01000110 after toggling the bit, then say the encryption key is '5', the end product for this code will be 1001011

now i have code for the encryption process, but the problem is that its not being outputted to the console, and i still dont really know how to debug my programs yet so i cant tell if the values are being encrypted correctly...the encryption process starts at psuedo-op CHECK2

here's the code i have now



;;encrypt the message

CHECK2 AND R5, R3, #1 ; check for odd/even
BRp TOGGLE1 ; if low order bit is #1
ADD R3, R3, #1 ; toggle bit to #1 if #0
BRnzp NEXT ; go to NEXT

TOGGLE1 ADD R3, R3, #-1 ; toggle bit to #0 if #1

NEXT ADD R4, R3, R1 ; add R1 and R3
ADD R3, R3, R3 ; shift bit
BRnp CHECK2 ; loop back until no bits left


thanks in advance for any input :D
if google is not like a friend, it must be in some cases like a wife, :)

http://highered.mcgraw-hill.com/sites/0072467509/student_view0/lc-3_simulator_lab_manual.html

in unix version of the manual at page 16 explain how you can debug, at 17 how you can setup a break point and on the same page how to run step by step.
I guess you must download the version for your os, mine is linux, :)

I remember that you can toogle the bit without any jump, you just need to simulate a adition with 1 without carry the next bit in case are both 1 but without any check, :)
this way you will remain only with the last jump, br from your code.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum