Reputation:
I have a problem with my program in assembler (NASM). Program get from user string of characters and write them to array. Next program have to reverse the characters by ascii code (a=z, b=y, c=x etc.). My program reverse correct only first character from array.
For example: when I write 'abcd', I get 'z=>?'.
Can you help me?
My procedure:
odwroc:
MOV bx, TEKST
MOV cx, 0
looop:
XOR ax, ax
XOR dx, dx
ADD ax, 122
MOV dx, [bx]
SUB ax, dx
MOV dx, 25
SUB dx, ax
MOV ax, 122
SUB ax, dx
MOV [bx], ax
INC bx
INC cx
CMP cx, 255
JLE looop
RET
And array definitione:
TEKST db 255
db 0
TIMES 256 db 36
Upvotes: 2
Views: 677
Reputation: 11219
To complement @Sep Roland 's answer, here is another way, using lea
, which should run faster:
table_rot:
db 0, 1, 2, 3, 4, 5, 6, 7 \
8, 9, 10, 11, 12, 13, 14, 15 \
16, 17, 18, 19, 20, 21, 22, 23 \
24, 25, 26, 27, 28, 29, 30, 31 \
32, 33, 34, 35, 36, 37, 38, 39 \
40, 41, 42, 43, 44, 45, 46, 47 \
48, 49, 50, 51, 52, 53, 54, 55 \
56, 57, 58, 59, 60, 61, 62, 63 \
64, 90, 89, 88, 87, 86, 85, 84 \ ; 65 'A' becomes 90 'Z', decrements until 65 is reached
83, 82, 81, 80, 79, 78, 77, 76 \
75, 74, 73, 72, 71, 70, 69, 68 \
67, 66, 65, 91, 92, 93, 94, 95 \
96,122,121,120,119,118,117,116 \ ; 97 'a' becomes 122 'z', decrements until 97 is reached
115,114,113,112,111,110,109,108 \
107,106,105,104,103,102,101,100 \
99, 98, 97,123,124,125,126,127 \
odwroc:
lea di, [rel table_rot] ; remove 'rel' depending on OS
mov bx, TEKST + 2 ; assuming that the input was coming from DOS Buffered Input function 0Ah
movzx cx, byte [TEKST + 1] ; counter, same DOS assumption
looop:
mov al, byte[di + bx] ; will load the byte corresponding to the table and add value of the current letter.
mov [bx], al ; load value of AL register in BX current pointer location
inc bx ; increment bx pointer, ie: go to next letter
dec cx
jnz looop
ret
I will let the care of improving the loop sequence to another developer.~ Also I haven't tested it so please let me know if I forgot anything.
Upvotes: 1
Reputation: 39166
TEKST db 255 db 0 TIMES 256 db 36
Judging from how you defined the TEKST, I understand that the input was coming from DOS Buffered Input function 0Ah.
If this is indeed the case then the text will be stored at the adres TEKST + 2
. You'll need to write mov bx, TEKST + 2
.
The inputted string will rarely be 255 bytes long. Your loop should end when all the characters are processed. Change cmp cx, 255
by cmp cl, [TEKST+1]
.
The text is composed of characters that are exactly 1 byte each. All of your program treats them as words (2 bytes). That's clearly wrong.
This is your program with corrections. There's still room for improvements!
MOV bx, TEKST + 2
MOV cx, 0
looop:
MOV al, 122 ;'z'
SUB al, [bx]
MOV dl, 25 ;'z' - 'a'
SUB dl, al
MOV al, 122 ;'z'
SUB al, dl
MOV [bx], al
INC bx
INC cx
CMP cl, [TEKST + 1]
JB looop
Upvotes: 1