The code seems to work until it gets to work with ASCII numbers that are bigger than "16". I think it's a problem of how I'm accessing and calculating the indexes and the ASCII conversion.
I need to calculate the number of permutations and display each of the permutations to the user.
;-----------------------------------------------------------------
; M A C R O
;-----------------------------------------------------------------
display macro xxx
push ax
push dx
mov dx, offset xxx
mov ah, 9
int 21h
pop dx
pop ax
endm
;-----------------------------------------------------------------
; Definizione costanti
CR EQU 13 ; carriage return
LF EQU 10 ; line feed
DOLLAR EQU "$"
;-----------------------------------------------------------------
;
PILA SEGMENT STACK 'STACK' ; stack' segment definition
DB 64 DUP('STACK') ; stack is filled with the string "stack"
; per identificarlo meglio in fase di debug
PILA ENDS
;-----------------------------------------------------------------
;
DATI SEGMENT PUBLIC 'DATA' ; data segment definition
CRLF db CR,LF,DOLLAR
msg_p db "permutation:", DOLLAR
msg_n db "number:",DOLLAR
r db "0", DOLLAR ; the number of permutations
look_up db "0", "4", "1", "5", "8", "12", "9", "13", "17", "21", "24" , "28", "25","29", "26", "30", "27","31","18", "22", "19", "23", "10", "14", "11", "15", "2", "6", "3", "7" ; permutation law
string 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", DOLLAR ; string Ill apply the permutation on
stringainitial 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", DOLLAR ; constant to check if the permutation changed the string back to the starting point
DATI ENDS
;=================================================================
CSEG1 SEGMENT PUBLIC 'CODE'
MAIN proc far
ASSUME CS:CSEG1,DS:DATI,SS:PILA,ES:NOTHING; initialization
MOV AX, DATI
MOV DS, AX
XOR CX, CX ; using CX for indexes
xor ax, ax ; ax as r counter
MOV BX, OFFSET string
mov si, offset stringainitial
display string
display CRLF
cicle:
mov dx, offset look_up ; I use the register multiple times, so Ill initialize it each cicle
inc ax
push dx
push bx
call permucalc
pop bx
pop dx
display msg_p ; display string after permutation
display string
display CRLF
xor cx,cx
checkifokay: ; check if the string is the same as at the starting point
add bx, cx
add si, cx
mov di, [bx]
mov dx, [si]
sub bx, cx
sub si, cx
cmp di,dx
jne cicle
cmp cx, 32
je exit
inc cx
jmp checkifokay
exit:
mov bx, offset r
sub [bx], "0"
add [bx], ax
add [bx], "0"
display msg_n
display r
display CRLF
main endp
;-----------------------------------------------------------------
;
permucalc proc
push ax
push bx
push cx
push dx
push si
push bp
mov bp, sp
mov bx, [bp+14] ; (stringa)
mov si, [bp+16] ; (table)
xor cx,cx
cicleproc:
cmp cx, 32
je terminate
add bx, cx ; need this to access the correct index
sub [bx], "0" ; to transform the number from ASCII
mov ax, [bx]
xor ah, ah
add si, ax
mov dx, [si]
sub si, ax
xor dh, dh
mov [bx], dl
sub bx, cx ; need to go back to the starting offset
inc cx
jmp cicleproc
terminate:
pop bp
pop si
pop dx
pop cx
pop bx
pop ax
ret
endp
I tried isolating 8 bits and it seemed like it worked until the number got bigger than 16.