Assembly - cercando di invertire stringa, ma aggiunge un carattere aggiuntivo sulla stringa finale
Domanda
Sono piuttosto nuovo all'Assemblea (e la programmazione in generale, ad essere onesti). Sto cercando di giocare con lo stack. Lo scopo di questo codice:
- Prendere in una stringa, limitato a 80 caratteri
- Ristampa la stringa come immesso
- Stampa ogni carattere in quanto è spinto alla pila
- Stampa ogni carattere come è spuntato dallo stack
- stampare la stringa invertita.
Il codice non è riuscito l'ultimo passo.
Se la stringa inserita è "Help", si stamperà "pleHe". L'ultimo carattere della stringa finale è il 2 ° carattere della stringa originale.
Per favore aiutami a vedere dove sto rovinare a!
.data buffer WORD 81 DUP(0) byteCount WORD ? .code main PROC call Clrscr ;Clear screen RS: mov edx, OFFSET buffer ;Move String to edx mov cl, [SIZEOF buffer]-1 ;Set loop counter to (size of buffer) -1 call ReadString ;Read a User's String mov byteCount, ax ;Move the size of User's String to byteCount cmp byteCount, 80 ;Compare byteCount with 80 ja RS ;If byteCount is greater then 80, ask for another String call WriteString ;Write User's String to screen call Crlf ;New Line call reverseIt ;Reverse order of String exit reverseIt PROC movzx ecx, byteCount ;Set Loop1 Counter to size of String mov esi, 0 ;Zero out ESI L1: ;Loop1 - Pushes String into Stack one character at a time movzx eax, buffer[esi] ;Dereference buffer and place in eax call Crlf ;New Line call WriteChar ;Print current character to screen push eax ;Push current character to stack inc esi ;Move to next character loop L1 call Crlf movzx ecx, byteCount ;Set Loop2 Counter to size of String mov esi, 0 ;Zero out ESI L2: ;Loop2 - Pops Characters back into String in reverse order pop eax ;Retrieve character from top of stack call Crlf ;New Line call WriteChar ;Print current character to screen mov buffer[esi], ax ;Writes character to String inc esi ;Increase esi loop L2 call Crlf ;New Line call Crlf ;New Line mov edx, OFFSET buffer ;Move String to edx for WriteString call WriteString ;Prints String to Screen call Crlf ;New Line ret ;Return to main reverseIt ENDP main ENDP END main
Soluzione
problema
Si sta trattando i vostri caratteri ASCII come parole piuttosto che byte, così si finisce per invertire due caratteri alla volta:
Quando si inverte la stringa due caratteri alla volta, si finisce per scrivere questi valori nel buffer:
esi+0: p-
esi+1: lp
esi+2: el
esi+3: He
Durante ogni iterazione il buffer si presenta in questo modo:
Help--
p-lp--
plpp--
plel--
pleHe-
Così si finisce per iscritto che in più e al buffer. Suppongo che l'e non compare nel vostro ciclo WriteChar.
Soluzione
Non ho ancora testato il codice in modo non può sapere con certezza, ma sembra che è necessario modificare questa riga:
mov buffer[esi], ax ;Writes character to String
a
mov ptr byte buffer[esi], al ;Writes character to String
E 'probabilmente una buona idea per cambiare questa linea troppo:
buffer WORD 81 DUP(0)
in modo che utilizza byte invece:
buffer BYTE 81 DUP(0)
Altri suggerimenti
Grazie per questo! Sapevo che stavo usando le parole invece di bytes perché la prima parte, in cui si limita la dimensione della stringa, non funzionava con i byte. Si stava tagliando la corda nel mezzo.
Quindi non ho cambiato buffer BYTE.
Detto questo, ho provato a fare l'altro cambiamento (che penso che avevo provato prima, pure), ma continuato a ottenere un errore di compilazione che indica entrambi gli operandi deve essere la stessa dimensione.
Ho fissato questo lanciando tampone [esi] come un byte! Ora funziona perfettamente! Grazie!
mov byte ptr buffer[esi], al