2016-04-16 4 views
-1

Ich programmiere mein erstes Assembly-Programm, und ich möchte die Länge eines Strings finden, am Ende konvertiere ich das Ergebnis, um es anzuzeigen, aber die Ergebnisse stammen nur aus 1 bis 9.Assembly gibt nicht mehr als 9 in meinem Ergebnis aus

Ich konvertiere die Variablen in Dezimal vor den Operationen und dann wandle ich sie in ASCII um sie anzuzeigen.

Ich habe versucht, nur das Ergebnis zu konvertieren, aber es funktioniert auch nicht.

Ich verwende NASM und Ausführen der Programme auf Ubuntu Linux.

Mein Code:

segment .data     ;Segmento de datos 
    mensaje db 'Introduzca una cadena: '  ;Mensaje 
    tamMensaje equ $-mensaje   ;Tamaño del mensaje 
    respuesta db 'Fue: ' 
    respuesta2 db 'Tamaño: ' 
    tamRespuesta equ $-respuesta 
    tamRespuesta2 equ $-respuesta2 
segment .bss 
    pal resb 50 ;Word 
    tam resb 1 ;Size of the word 
segment .text 
global _start 

_start: 
    mov eax, 4 
    mov ebx, 1 
    mov ecx, mensaje 
    mov edx, tamMensaje 
    int 0x80 

;Leer 
    mov eax, 3 
    mov ebx, 2 
    mov ecx, pal 
    mov edx, 50 
    int 0x80 

;Respuesta 
    mov eax, 4 
    mov ebx, 1 
    mov ecx, respuesta 
    mov edx, tamRespuesta 
    int 0x80 

;Respuesta Numero 
    mov eax, 4 
    mov ebx, 1 
    mov ecx, pal 
    mov edx, 50 
    int 0x80 

;Count size 
    mov edi, pal  ;move word to edi 
    sub ecx, ecx  ;set ecx to 0 
    not ecx   ;set ecx to highest value 
    sub ecx, '0'  ; conversion to decimal 
    sub al, al  ; set al to zero, looks for a null character 
    cld    ;clear flags 
    repne scasb  ; decreases ecx moving trough the word 
    mov eax,0fffffffeh ;set eax to highest value minus 1 
    sub eax, '0'  ;conversion to decimal 
    sub eax,ecx  ; lenght = eax - ecx 
    dec eax   ; eax = eax -1 
    add eax, '0'  ; conversion to ascii 
    mov [tam],eax 
    int 0x80  

;Imprimir mensaje -tamaño:- 
    mov eax, 4 
    mov ebx, 1 
    mov ecx, respuesta2 
    mov edx, tamRespuesta2 
    int 0x80 


;Imprimir Tamaño 
    mov ecx, tam 
    mov ebx, 1 
    mov edx, 1 
    mov eax, 4 
    int 0x80 



;salir 
    mov eax,1 
    mov ebx,0 
    int 0x80 
+0

"Funktioniert nicht" sagt uns nicht viel. Was ist der genaue Fehler oder die fehlerhafte Ausgabe, die Sie erhalten? (Siehe [Wie man fragt] [http://www.stackoverflow.com/help/how-to-ask).) – CodeMouse92

+0

Entschuldigung, das habe ich nicht erklärt. Wenn die Größe 9 oder niedriger ist. Die Nummer ist gedruckt, aber wenn die Größe höher ist, druckt es Buchstaben oder sogar Leerzeichen – Raphael

+0

Ich möchte hinzufügen, dass, da diese Website meist Englisch-Lautsprecher ist, Sie eine bessere Antwort erhalten können, wenn die Variablennamen und Kommentare übersetzt werden. Es erspart uns die Arbeit, Google Übersetzer zu öffnen. –

Antwort

0

Untersuchen Sie diese (oder eine) ascii table, und Sie werden feststellen, dass es für die Zahlen 0 bis 9 (ab ascii # 48) nur ein ascii entspricht. Wenn Sie beispielsweise '0' + 10 erreichen, werden Sie mit :, ;, < und so weiter enden.

Somit funktioniert add eax, '0' gut für einstellige Dezimalzahlen, aber Sie müssen diesen Vorgang für jedes Vielfache von 10 wiederholen, beginnend mit der höchstwertigen Ziffer.

Sie können dies iterativ tun:

//num is any number, in your case, the length 
for (int num=1026; num>0; num=Math.floor(num/10)) 
    stdout('0' + (num % 10)); //not a real function 

Aber Sie werden rückwärts Ihre Ergebnisse erhalten. Sie könnten in ein Array speichern und umgekehrt, oder Sie können eine rekursive Schleife verwenden statt:

void translateChar(int num) { 
    if (num <= 0) return; 
    translateChar(num/10); //non-float division, very important 
    stdout('0'+(num%10)); //this function isn't real, of course 
    return; 
} 

translateChar(1025); //whatever your length is 

Es scheint, wie Sie einen guten Überblick über NASM haben, also werde ich die Übersetzung der Montage dem Leser überlassen.

Erstes Beispiel: http://pythontutor.com/javascript.html#code=var+out%3D%5B%5D%3B%0D%0Afor+(var+n%3D1026%3Bn%3E0%3Bn%3DMath.floor(n/10%29%29+out.push(num%2510%29%3B&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=js&rawInputLstJSON=%5B%5D&curInstr=0

rekursive Beispiel: http://pythontutor.com/javascript.html#code=var+out%3D%5B%5D%3B%0D%0Afunction+toChar(n%29+%7B%0D%0A++if(n%3C%3D0%29+return%3B%0D%0A++toChar(Math.floor(n/10%29%29%3B%0D%0A++out.push(n%2510%29%3B%0D%0A++return%3B%0D%0A%7D%0D%0AtoChar(1023%29%3B&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=js&rawInputLstJSON=%5B%5D&curInstr=29

Big dank SO für mich diese als Inline-Links nicht im Stich gelassen posten.

Verwandte Themen