tasm - assembly reverse a string -


the string read until 1 pressed, , 1 on last position of string. don't know why output off, example input is: asd1 , output is: $1111. anyway, here code

data segment msg db 0dh,0ah,"your string: $" rev db 0dh,0ah,"reverted: $" s1 db 20 dup('$') s2 db 20 dup('$') data ends  code segment assume cs:code,ds:data  start: mov ax,data mov ds,ax  lea dx,msg mov ah,09h int 21h  lea si,s1 lea di,s2 mov bx,0 l1: mov ah,01h int 21h mov [si],al inc bx inc si cmp al,31h jnz l1   mov cx,bx mov di,bx  dec1:   dec si loop dec1  mov cx,bx l2: mov al,[si] mov [di],al dec di inc si loop l2   lea dx,rev mov ah,09h int 21h  mov cx,bx l3: mov ah,02h mov dl,[di] int 21h inc di loop l3   mov ah,4ch int 21h   code ends end start 

edit:this code looks , if input asd1, 1dserted

edit: after ped7g's comment, reworked code. new 1 doesn't use stack reverse string , string not read whole string, read char char until "enter" pressed. below new code.

assume cs:code, ds:data  data segment     message db 0dh, 0ah, "string: $"     reverse db 0dh, 0ah, "result: $"     string db 255 dup(0)     result db 255 dup('$') data ends  code segment start:     mov ax, data     mov ds, ax      ; print "string: "     mov ah, 09h     lea dx, message     int 21h      ; set si read string     lea si, string      read:         ; read single character keyboard         mov ah, 01h         int 21h          ; save in memory         mov [si], al         inc si          ; check if enter pressed (if not, repeat reading)         cmp al, 0dh         jnz read      ; calculate length of string read     mov ax, si     lea bx, string     sub ax, bx      ; set di @ last char of result     lea di, result     add di, ax      ; decrement 1 byte position di on last char     ; of string (the carriage return)     dec di      ; decrement 1 byte because don't want consider     ; carriage return part of our reversed string     dec di      ; set si @ first char of string     lea si, string      reverse_string:         ; copy beginning of initial string         ; end of reversed string         mov al, [si]         mov [di], al          ; step         inc si         dec di          ; verify if have reached end of initial string         ; (if "current" char carriage return)         cmp byte ptr [si], 0dh         jnz reverse_string      ; print "result: "     mov ah, 09h     lea dx, reverse     int 21h       write:         ; write whole reversed string on standard output         mov ah, 09h         lea dx, result         int 21h      mov ah, 4ch     int 21h code ends  end start 

old answer:

you can try use lifo property of stack. below example of code reverses string using it. algorithm puts every character beginning of input string, , pops out result (in reverse order).

assume cs:code, ds:data  data segment      msg db 0dh, 0ah, "string: $"      rev db 0dh, 0ah, "result: $"      buffer label byte     str_maxlen db 255     str_length db 0     str_string db 255 dup(0)      result db 255 dup('$')  data ends  code segment  start:     mov ax,data     mov ds,ax      mov ah, 09h     lea dx, msg     int 21h         ; print "your string"      mov ah, 0ah     lea dx, buffer     int 21h         ; read string      cmp str_length, 0     je skip         ; check if input null      mov ch, 0     mov cl, str_length     lea si, str_string     put_on_stack:         push [si]   ; copy on stack (from string)         inc si         loop put_on_stack      mov ch, 0     mov cl, str_length     lea di, result     get_from_stack:         pop [di]    ; copy memory (in result)         inc di         loop get_from_stack      mov byte ptr [di], '$'      skip:     mov ah, 09h     lea dx, rev     int 21h         ; print "result: "      mov ah, 09h     lea dx, result     int 21h         ; print result      mov ah,4ch     int 21h  code ends  end start 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -