News:

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

Main Menu

Im newbie

Started by shekel, May 20, 2015, 02:58:07 PM

Previous topic - Next topic

shekel

Hi, my name is shekel and im from spain.. english is not my native language, so excusme if its difficult understand me.

I read the Iczelon's tutorials and i can understand all the code.. but now im trying to do something in asm for myself.

This is c++.. how can I do this in asm?

String var = "-Hello World-";
String var2 = var.substr(1, var.size() - 2);

This code remove the first and the last character, in asm i need duplicate the string and modify the result??? or can i mov into "esi" the string with a start and end position without "break" the original string?

Sorry by my english

dedndave

String var = "-Hello World-";

that one is easy enough
if it's global data, and assuming it is zero terminated...
    .DATA

var     db "-Hello World-",0


if it's local data, you can assign a buffer, but you have to put the string into the buffer, "manually"
MyFunc PROC

    LOCAL   var[16]   :BYTE

    mov dword ptr var,"leH-"
    mov dword ptr var+4,"W ol"
    mov dword ptr var+8,"dlro"
    mov word ptr var+12,"-"

;
;
    ret

MyFunc ENDP

there's probably a macro to do that - or there should be   :P

this one's a bit different - it requires that you execute some code
normally, we would write a function to perform the operation, and pass pointers to the string and buffer, and perhaps a length
String var2 = var.substr(1, var.size() - 2);
there's probably also a macro for that

but, to copy a string, i might use the string instructions
    push    esi
    push    edi
    mov     esi,<address of source>
    mov     edi,<address of destination>
    mov     ecx,<number of elements to copy>    ;bytes, words, or dwords
    rep     movsb                               ;or movsw for words, or movsd for dwords
    pop     edi
    pop     esi

you can modify that, as required, to copy only the bytes you want into the destination buffer

jj2007

Dave is a bit demanding today :biggrin:

Here is a simpler approach:
include \masm32\include\masm32rt.inc

.data
var1 db "-Hello World-", 0
var2 db 100 dup(?) ; generous buffer for the copy

.code
start:
  mov esi, offset var1
  mov edi, offset var2
  inc esi
  mov ecx, sizeof var1-3 ; subtract 1 for the inc esi, 1 for the right end, and 1 for the zero delimiter
  rep movsb
  inkey offset var2
  exit

end start


Read \Masm32\help\opcodes.chm to understand rep movsb

And have a look at this two-pager, too.