Oppure

Loading
11/03/11 19:40
Pitagora
Salve, ho da poco iniziato l'apprendimento dell' assembly con sintassi AT&T. Voglio provare a crea un programma, che faccia quanto segue: Data una stringa in input, stampare il suo contrario. In C io farei una cosa del genere:

#include <stdio.h>
#include <string.h>

int main (void) {
	char stringa[20];
	int i;
	printf("Inserisci stringa: ");
	fgets(stringa, sizeof(stringa), stdin);
	stringa[strlen(stringa)-1] = 'Salve, ho da poco iniziato l'apprendimento dell' assembly con sintassi AT&T. Voglio provare a crea un programma, che faccia quanto segue: Data una stringa in input, stampare il suo contrario. In C io farei una cosa del genere:


#include <stdio.h>
#include <string.h>

int main (void) {
	char stringa[20];
	int i;
	printf("Inserisci stringa: ");
	fgets(stringa, sizeof(stringa), stdin);
	stringa[strlen(stringa)-1] = '{parsed_message}';
	printf("Stringa al contrario: ");
	for (i = strlen(stringa); i >= 0; i--) {
		printf("%c", stringa[i]);
	}
	printf("\n");
	return 0;
}

In assembly non riesco a capire come piazzare in un registro ad esempio l'ultimo carattere della stringa, il penultimo carattere della stringa, il terzultimo carattere della stringa, ecc. Io avevo pensato ad una cosa simile:
movb stringa[n], %ecx

ovvero spostare un byte a partire da stringa[n], nel registro ECX ma non funge... Soluzioni?

Ultima domanda che differenza c'è nello scrivere:
decl var

e
movl $var, %eax
decl %eax
'; printf("Stringa al contrario: "); for (i = strlen(stringa); i >= 0; i--) { printf("%c", stringa[i]); } printf("\n"); return 0; }

In assembly non riesco a capire come piazzare in un registro ad esempio l'ultimo carattere della stringa, il penultimo carattere della stringa, il terzultimo carattere della stringa, ecc. Io avevo pensato ad una cosa simile:
movb stringa[n], %ecx

ovvero spostare un byte a partire da stringa[n], nel registro ECX ma non funge... Soluzioni?

Ultima domanda che differenza c'è nello scrivere:
decl var

e
movl $var, %eax
decl %eax
Ultima modifica effettuata da Pitagora 11/03/11 19:44
aaa
12/03/11 20:44
lumo
Rispondo all'ultima domanda: dec vuole un registro come argomento
riguardo a string[n], pensalo come *(string + n*sizeof(char)). In pratica ti basta mettere string in un registro, o meglio, l'indirizzo al primo elemento della stringa. Dopo in un altro registro metti 0. Dopodichè usi il meccanismo che ho scritto sopra per ottenere il valore a quell'indirizzo di memoria, e per scorrere la stringa basta che incrementi questo registro. Probabilmente ci sono modi migliori ma l'assembly non l'ho mai approfondito.
aaa
13/03/11 11:02
Pitagora
in modo più semplice:
movl $str, %ecx
addl , %ecx

è uguale a scrivere
*(stringa += 3)

Puntano entrambe all'elemento 3* della stringa.

Grazie lo stesso. :k:
aaa
16/03/11 18:33
Pitagora
Per chi avrà problemi in futuro, inserisco qui il codice:

.data
      welcome: .string "Inserisci stringa "
      n_welcome = .-welcome
      sms: .string "Stringa ribaltata "
      n_sms = .-sms
      str: .space 30 # Lunghezza stringa
      n_str = .-str
      new_line: .string "\n"
.text
      .global main
main:
      # STAMPO: "Inserisci stringa "
      movl , %eax
      movl , %ebx
      movl $welcome, %ecx
      movl $n_welcome, %edx
      int Per chi avrà problemi in futuro, inserisco qui il codice:


.data
      welcome: .string "Inserisci stringa "
      n_welcome = .-welcome
      sms: .string "Stringa ribaltata "
      n_sms = .-sms
      str: .space 30 # Lunghezza stringa
      n_str = .-str
      new_line: .string "\n"
.text
      .global main
main:
      # STAMPO: "Inserisci stringa "
      movl , %eax
      movl , %ebx
      movl $welcome, %ecx
      movl $n_welcome, %edx
      int {parsed_message}x080
      
      # LEGGO: str 
      movl , %eax
      movl {parsed_message}, %ebx
      movl $str, %ecx
      movl $n_str, %edx
      int {parsed_message}x080
      
      # STAMPO: "Stringa ribaltata " 
      movl , %eax
      movl , %ebx
      movl $sms, %ecx
      movl $n_sms, %edx
      int {parsed_message}x080
      
      # COPIO: lunghezza str nel registro ESI
      movl $n_str, %esi
 
 loop:
      movl , %eax
      movl , %ebx
      movl $str, %ecx
      addl %esi, %ecx
      movl , %edx
      int {parsed_message}x080
      
      decl %esi
      
      cmp $-1, %esi
      jne loop
 
 exit:
      movl , %eax
      movl , %ebx
      movl $new_line, %ecx
      movl , %edx
      int {parsed_message}x080
      
      movl , %eax
      movl {parsed_message}, %ebx
      int {parsed_message}x080
:rofl:x080 # LEGGO: str movl , %eax movl Per chi avrà problemi in futuro, inserisco qui il codice:

.data
      welcome: .string "Inserisci stringa "
      n_welcome = .-welcome
      sms: .string "Stringa ribaltata "
      n_sms = .-sms
      str: .space 30 # Lunghezza stringa
      n_str = .-str
      new_line: .string "\n"
.text
      .global main
main:
      # STAMPO: "Inserisci stringa "
      movl , %eax
      movl , %ebx
      movl $welcome, %ecx
      movl $n_welcome, %edx
      int {parsed_message}x080
      
      # LEGGO: str 
      movl , %eax
      movl {parsed_message}, %ebx
      movl $str, %ecx
      movl $n_str, %edx
      int {parsed_message}x080
      
      # STAMPO: "Stringa ribaltata " 
      movl , %eax
      movl , %ebx
      movl $sms, %ecx
      movl $n_sms, %edx
      int {parsed_message}x080
      
      # COPIO: lunghezza str nel registro ESI
      movl $n_str, %esi
 
 loop:
      movl , %eax
      movl , %ebx
      movl $str, %ecx
      addl %esi, %ecx
      movl , %edx
      int {parsed_message}x080
      
      decl %esi
      
      cmp $-1, %esi
      jne loop
 
 exit:
      movl , %eax
      movl , %ebx
      movl $new_line, %ecx
      movl , %edx
      int {parsed_message}x080
      
      movl , %eax
      movl {parsed_message}, %ebx
      int {parsed_message}x080
:rofl:, %ebx movl $str, %ecx movl $n_str, %edx int Per chi avrà problemi in futuro, inserisco qui il codice:

.data
      welcome: .string "Inserisci stringa "
      n_welcome = .-welcome
      sms: .string "Stringa ribaltata "
      n_sms = .-sms
      str: .space 30 # Lunghezza stringa
      n_str = .-str
      new_line: .string "\n"
.text
      .global main
main:
      # STAMPO: "Inserisci stringa "
      movl , %eax
      movl , %ebx
      movl $welcome, %ecx
      movl $n_welcome, %edx
      int {parsed_message}x080
      
      # LEGGO: str 
      movl , %eax
      movl {parsed_message}, %ebx
      movl $str, %ecx
      movl $n_str, %edx
      int {parsed_message}x080
      
      # STAMPO: "Stringa ribaltata " 
      movl , %eax
      movl , %ebx
      movl $sms, %ecx
      movl $n_sms, %edx
      int {parsed_message}x080
      
      # COPIO: lunghezza str nel registro ESI
      movl $n_str, %esi
 
 loop:
      movl , %eax
      movl , %ebx
      movl $str, %ecx
      addl %esi, %ecx
      movl , %edx
      int {parsed_message}x080
      
      decl %esi
      
      cmp $-1, %esi
      jne loop
 
 exit:
      movl , %eax
      movl , %ebx
      movl $new_line, %ecx
      movl , %edx
      int {parsed_message}x080
      
      movl , %eax
      movl {parsed_message}, %ebx
      int {parsed_message}x080
:rofl:x080 # STAMPO: "Stringa ribaltata " movl , %eax movl , %ebx movl $sms, %ecx movl $n_sms, %edx int Per chi avrà problemi in futuro, inserisco qui il codice:

.data
      welcome: .string "Inserisci stringa "
      n_welcome = .-welcome
      sms: .string "Stringa ribaltata "
      n_sms = .-sms
      str: .space 30 # Lunghezza stringa
      n_str = .-str
      new_line: .string "\n"
.text
      .global main
main:
      # STAMPO: "Inserisci stringa "
      movl , %eax
      movl , %ebx
      movl $welcome, %ecx
      movl $n_welcome, %edx
      int {parsed_message}x080
      
      # LEGGO: str 
      movl , %eax
      movl {parsed_message}, %ebx
      movl $str, %ecx
      movl $n_str, %edx
      int {parsed_message}x080
      
      # STAMPO: "Stringa ribaltata " 
      movl , %eax
      movl , %ebx
      movl $sms, %ecx
      movl $n_sms, %edx
      int {parsed_message}x080
      
      # COPIO: lunghezza str nel registro ESI
      movl $n_str, %esi
 
 loop:
      movl , %eax
      movl , %ebx
      movl $str, %ecx
      addl %esi, %ecx
      movl , %edx
      int {parsed_message}x080
      
      decl %esi
      
      cmp $-1, %esi
      jne loop
 
 exit:
      movl , %eax
      movl , %ebx
      movl $new_line, %ecx
      movl , %edx
      int {parsed_message}x080
      
      movl , %eax
      movl {parsed_message}, %ebx
      int {parsed_message}x080
:rofl:x080 # COPIO: lunghezza str nel registro ESI movl $n_str, %esi loop: movl , %eax movl , %ebx movl $str, %ecx addl %esi, %ecx movl , %edx int Per chi avrà problemi in futuro, inserisco qui il codice:

.data
      welcome: .string "Inserisci stringa "
      n_welcome = .-welcome
      sms: .string "Stringa ribaltata "
      n_sms = .-sms
      str: .space 30 # Lunghezza stringa
      n_str = .-str
      new_line: .string "\n"
.text
      .global main
main:
      # STAMPO: "Inserisci stringa "
      movl , %eax
      movl , %ebx
      movl $welcome, %ecx
      movl $n_welcome, %edx
      int {parsed_message}x080
      
      # LEGGO: str 
      movl , %eax
      movl {parsed_message}, %ebx
      movl $str, %ecx
      movl $n_str, %edx
      int {parsed_message}x080
      
      # STAMPO: "Stringa ribaltata " 
      movl , %eax
      movl , %ebx
      movl $sms, %ecx
      movl $n_sms, %edx
      int {parsed_message}x080
      
      # COPIO: lunghezza str nel registro ESI
      movl $n_str, %esi
 
 loop:
      movl , %eax
      movl , %ebx
      movl $str, %ecx
      addl %esi, %ecx
      movl , %edx
      int {parsed_message}x080
      
      decl %esi
      
      cmp $-1, %esi
      jne loop
 
 exit:
      movl , %eax
      movl , %ebx
      movl $new_line, %ecx
      movl , %edx
      int {parsed_message}x080
      
      movl , %eax
      movl {parsed_message}, %ebx
      int {parsed_message}x080
:rofl:x080 decl %esi cmp $-1, %esi jne loop exit: movl , %eax movl , %ebx movl $new_line, %ecx movl , %edx int Per chi avrà problemi in futuro, inserisco qui il codice:

.data
      welcome: .string "Inserisci stringa "
      n_welcome = .-welcome
      sms: .string "Stringa ribaltata "
      n_sms = .-sms
      str: .space 30 # Lunghezza stringa
      n_str = .-str
      new_line: .string "\n"
.text
      .global main
main:
      # STAMPO: "Inserisci stringa "
      movl , %eax
      movl , %ebx
      movl $welcome, %ecx
      movl $n_welcome, %edx
      int {parsed_message}x080
      
      # LEGGO: str 
      movl , %eax
      movl {parsed_message}, %ebx
      movl $str, %ecx
      movl $n_str, %edx
      int {parsed_message}x080
      
      # STAMPO: "Stringa ribaltata " 
      movl , %eax
      movl , %ebx
      movl $sms, %ecx
      movl $n_sms, %edx
      int {parsed_message}x080
      
      # COPIO: lunghezza str nel registro ESI
      movl $n_str, %esi
 
 loop:
      movl , %eax
      movl , %ebx
      movl $str, %ecx
      addl %esi, %ecx
      movl , %edx
      int {parsed_message}x080
      
      decl %esi
      
      cmp $-1, %esi
      jne loop
 
 exit:
      movl , %eax
      movl , %ebx
      movl $new_line, %ecx
      movl , %edx
      int {parsed_message}x080
      
      movl , %eax
      movl {parsed_message}, %ebx
      int {parsed_message}x080
:rofl:x080 movl , %eax movl Per chi avrà problemi in futuro, inserisco qui il codice:

.data
      welcome: .string "Inserisci stringa "
      n_welcome = .-welcome
      sms: .string "Stringa ribaltata "
      n_sms = .-sms
      str: .space 30 # Lunghezza stringa
      n_str = .-str
      new_line: .string "\n"
.text
      .global main
main:
      # STAMPO: "Inserisci stringa "
      movl , %eax
      movl , %ebx
      movl $welcome, %ecx
      movl $n_welcome, %edx
      int {parsed_message}x080
      
      # LEGGO: str 
      movl , %eax
      movl {parsed_message}, %ebx
      movl $str, %ecx
      movl $n_str, %edx
      int {parsed_message}x080
      
      # STAMPO: "Stringa ribaltata " 
      movl , %eax
      movl , %ebx
      movl $sms, %ecx
      movl $n_sms, %edx
      int {parsed_message}x080
      
      # COPIO: lunghezza str nel registro ESI
      movl $n_str, %esi
 
 loop:
      movl , %eax
      movl , %ebx
      movl $str, %ecx
      addl %esi, %ecx
      movl , %edx
      int {parsed_message}x080
      
      decl %esi
      
      cmp $-1, %esi
      jne loop
 
 exit:
      movl , %eax
      movl , %ebx
      movl $new_line, %ecx
      movl , %edx
      int {parsed_message}x080
      
      movl , %eax
      movl {parsed_message}, %ebx
      int {parsed_message}x080
:rofl:, %ebx int Per chi avrà problemi in futuro, inserisco qui il codice:

.data
      welcome: .string "Inserisci stringa "
      n_welcome = .-welcome
      sms: .string "Stringa ribaltata "
      n_sms = .-sms
      str: .space 30 # Lunghezza stringa
      n_str = .-str
      new_line: .string "\n"
.text
      .global main
main:
      # STAMPO: "Inserisci stringa "
      movl , %eax
      movl , %ebx
      movl $welcome, %ecx
      movl $n_welcome, %edx
      int {parsed_message}x080
      
      # LEGGO: str 
      movl , %eax
      movl {parsed_message}, %ebx
      movl $str, %ecx
      movl $n_str, %edx
      int {parsed_message}x080
      
      # STAMPO: "Stringa ribaltata " 
      movl , %eax
      movl , %ebx
      movl $sms, %ecx
      movl $n_sms, %edx
      int {parsed_message}x080
      
      # COPIO: lunghezza str nel registro ESI
      movl $n_str, %esi
 
 loop:
      movl , %eax
      movl , %ebx
      movl $str, %ecx
      addl %esi, %ecx
      movl , %edx
      int {parsed_message}x080
      
      decl %esi
      
      cmp $-1, %esi
      jne loop
 
 exit:
      movl , %eax
      movl , %ebx
      movl $new_line, %ecx
      movl , %edx
      int {parsed_message}x080
      
      movl , %eax
      movl {parsed_message}, %ebx
      int {parsed_message}x080
:rofl:x080
:rofl:
aaa