News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

filling arrays

Started by grimx, August 11, 2007, 10:22:41 PM

Previous topic - Next topic

grimx

trying to put value stored in [letter] into the array index of [buffer]
how do i do it??



.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

GetString proto

.data

  MsgBoxCaption  db "Iczelion Tutorial No.2",0
  MsgBoxText     db "Win32 Assembly is Great",0
  buffer  db 26 dup(0)
  letter db "A"

.code
start:
  invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK
  invoke MessageBox, NULL, addr buffer, addr buffer, MB_OK
  invoke GetString   ;Calling GetString function
  invoke MessageBox, NULL, addr buffer, addr buffer, MB_OK

  invoke ExitProcess,NULL

;---------------------GetString Function----------------------
GetString proc
 
  mov ecx, 10
  mov eax, 0
  mov esi, 0
  GetLetter:
    ;--------- where do i store letter so i can put it in buffer[x]
    mov [byte ptr buffer + esi + eax ] ,  ;------- the  new letter here
    inc eax
    inc [letter]  ;--------------inc letter  A - Z
  Loop GetLetter
ret
GetString endp
;--------------------End of GetString Function----------------------------

end start


grimx

ok i think i got it



.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

GetString proto

.data

  MsgBoxCaption  db "Iczelion Tutorial No.2",0
  MsgBoxText     db "Win32 Assembly is Great",0
  buffer  db 26 dup("a"), 0
  letter db "A", 0

.code
start:
  invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK

  ; THIS SHOWS THE BUFFER
  invoke MessageBox, NULL, addr buffer, addr buffer, MB_OK 

  invoke GetString   ;Calling GetString function---- I FIXED THIS IT PUTS A-Z IN buffer

  ; PRINTS buffer A - Z
  invoke MessageBox, NULL, addr buffer, addr buffer, MB_OK
;

  invoke ExitProcess,NULL

;---------------------GetString Function----------------------
GetString proc
 
  mov ecx, 26
  mov eax, 0
  mov esi, 0
 
  GetLetter:
    mov dl, [letter] ;---------dl = letter
    mov [byte ptr buffer + esi + eax ] , dl  ;--put new letter in buffer
    inc eax  ;---increment buffer[esi]  esi + eax
    inc [letter]  ;------inc letter  A - Z
  Loop GetLetter
ret
GetString endp
;--------------------End of GetString Function----------------------------

end start



would this be the correct way of filling buffer[] with letter??

MichaelW

It works, so it is "a" way, but "correct" depends on what your goal is. You could accomplish the same thing with fewer registers. Unless there is some reason to update letter, you could just increment the value in DL. Except for the early processors, LOOP is slower than the equivalent:

dec  ecx
jnz  loop_label

And unlike the equivalent, LOOP can accept only a "short" label, so the jump destination cannot be further back than -128 from the instruction following the LOOP instruction.

eschew obfuscation

OldTimer

Hi,
   I like to keep things simple and readable whilst avoiding the use of labels.

      mov   edi,offset Buffer       ; put letters here
      mov   al,'A'                       ; start at "A"
      .while  al != 'Z'+1              ; end at "Z"
        stosb 
        inc al
      .endw


Just 1 register but a bit more complicated.
     
      mov   eax,'A'                              ; start at "A"
      .while  al != 'Z'+1                        ; end at "Z"
        mov byte ptr [Buffer+eax-'A'],al
        inc al
      .endw

   In some systems LOOP and JNZ are both short jumps but while/endw works it all out.

Have fun.

MichaelW

QuoteIn some systems LOOP and JNZ are both short jumps...
Yes, but for conditional jumps when the processor directive is .386 or higher, necessary for 32-bit code, MASM will encode the shortest jump possible, either short or near, but LOOP is only short.

eschew obfuscation