News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

help reversing characters and revirsing an array using loops

Started by ratten, March 31, 2018, 05:58:44 PM

Previous topic - Next topic

ratten

Hi I am having a little bit of trouble making this programs the way I want them

in the first one I need to input 10 characters and then reverse them and show the output I have this:

INCLUDE Irvine32.inc
WriteString PROTO


.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP('#')

.code
main PROC
mov esi,0
mov edi,LENGTHOF source - 2
mov ecx,SIZEOF source
L1:
mov al,source[esi]
mov target[edi],al
inc esi
dec edi
loop L1
mov edx, OFFSET target
call WriteString

exit
main ENDP
END main

And in the second one I need to use a loop and indexed addressing and rotates the members an array forward one position. This is what I got but I need to display the array before and after the rotation something like Array before Rotation: 1 x x 4 , Array after rotation: 4 1 x x and also need to input the 4 numbers with the keyboard:

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
array DWORD 10,20,30,40
arrayType DWORD TYPE array
newArray DWORD LENGTHOF array DUP(?)
lastElement DWORD ?

.code
main PROC
   
   ;Get first element address in ESI
   MOV ESI, OFFSET array

   ;Get address of next element in EDI
   MOV EDI, OFFSET newArray
   ADD EDI, TYPE newArray

   ;set loop count into ecx
   mov ECX, LENGTHOF array

L2:
   MOV EAX, [ESI]
   MOV [EDI], EAX

   ADD ESI, TYPE array
   ADD EDI, TYPE array

   LOOP L2

   ;set last element from array in newArray first position
   MOV EDI,OFFSET newArray
   MOV EAX, [ESI]
   MOV [EDI], EAX

INVOKE ExitProcess,0
main ENDP
END main

hutch--

This will save you a lot of trouble.

szRev proc src:DWORD,dst:DWORD

It is in the MASM32 library.

jj2007

Usage example ;)include \masm32\MasmBasic\MasmBasic.inc

.data?
dest db 100 dup(?)

  Init

  invoke szRev, chr$("This will save you a lot of trouble"), addr dest

  print offset dest, 13, 10
  Inkey "or use this one: ", CrLf$, Mirror$("nibtsud eht otni yrarbil enivrI eht worht dluohs yllaer uoY")
  invoke ExitProcess, 0
end start


If you want some "real" assembler code, though, here is something more elaborated:  Let esi="This is the source string"    ; with the Irvine lib, use mov esi, offset mysource
  mov edi, offset dest
  xor ecx, ecx
  xor edx, edx
  .Repeat
inc edx
  .Until byte ptr [esi+edx]==0
  .Repeat
dec edx
mov al, [esi+edx]
mov [edi+ecx], al
inc ecx
  .Until sdword ptr edx<0
  mov byte ptr [edi+ecx-1], 0
  print offset dest, 13, 10

ratten

Hi I got the first program to work, but the second one I think its ok I just need to show the array before and after it executes