linux-assembly.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* confused asm newbie
@ 2003-10-23 12:03 Jason Roberts
  2003-10-23 12:53 ` Frederic Marmond
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Jason Roberts @ 2003-10-23 12:03 UTC (permalink / raw)
  To: linux-assembly

I have a few questions concerning how the stack is used and how memory is 
accessed
and what it looks like.I've read books, which only serve to confuse me, or 
the author tries
to mystify you into believing assembly is dark magic only for the elite.
Thanks in advance for any help and/or suggestions--

   in C  If I code char *p = "hello" then *p will be 'h' and *(p+1) will be 
'e', and likewise
   p[4] will derefrence byte 5 ,which is 'o'.If I  ++p  the value at p  will 
be whatever is at the address        of the next byte,etc

   now, how does this relate to asm registers???
   based on code below  'msg' (line 5) is a pointer or 'label' which 
represents an address which             holds 3              bytes plus 
LF,CF,and NULL.
   based on my current understanding--
    msg = address of first byte
   [msg] = value at base,'a'
   add [msg],4 adds ascii value 4 to 'a' so it's now 'e'
   add msg,3   adds 3 to pointer, so msg is now base+3, so  now [msg] should 
be 'c'
   am I right on this???

    also, when I used edi to store string in _start (line 61)  I mov'ed 
'string' without the brackets
    for some reason the use of brackets is neccessary in the case of
    msg (line 84) ...why so?
    also, the stack issue:
    based on my knowledge the stack grows downward but reads upward, i.e. if 
I push
    edi then I have pushed 4 bytes onto stack- and so sp decrements 4 times 
and points
    at last item pushed.  Sp only points to top of stack and knows nothing 
about memory below
    unless we tell it too by explicityly moving it down,which is allocating 
space basically,malloc()
    for the C gang. Am I getting it?
    My only concern is this:
    what does edi look like after line 84???
    we have 6 bytes going into a 4-byte register...
    my guess is:
    6162630a with the CR and NULL being ignored.
    If I'm right then what does the stack look like after the push?
    From what I see the values in the registers are pushed from the low-byte 
up to high so
    that 61 is on top of stack, or worded differently, esp holds the address 
of where 61 is.

    If I got all this right say Kudos! ,otherwise please point me narrow--
    Thankyou for your time and patience.
     Paul


line 1:  section .data
line 2:
line 3:  string     db "Hello insanity!",10,13,0
line 4:  error_msg  db "Null Pointer!",10,13,0
line 5:  msg        db "abc",10,13,0
line 6:
line 7:
line 8:
line 9:  section .text
line 10:
line 11:  global _start
line 12:
line 13:  jmp _start
line 14:
line 15:  ;routine which puts length of string at es:edi into ecx and 
returns
line 16:  get_len:
line 17:           jmp chk_edi
line 18:
line 19:  _inc:
line 20:           inc ecx
line 21:   jmp scan
line 22:
line 23:   ;check that edi is not null or scasb will fail
line 24:  chk_edi:
line 25:           or edi,edi
line 26:           cmovz eax,edi
line 27:   dec eax
line 28:           ret
line 29:
line 30:
line 31:  scan:
line 32:
line 33:   scasb
line 34:           jne _inc
line 35:           ret ; but what if first byte is null? well when ret we 
will check now wont we!
line 36:
line 37:
line 38:
line 39:  error:
line 40:
line 41:
line 42:   mov  edx,15     ;length of buffer
line 43:   mov  eax,4  ;sys_write
line 44:   mov  ebx,1       ;file descriptor
line 45:   mov  ecx,error_msg  ;buffer
line 46:
line 47:   int  0x80
line 48:
line 49:
line 50:           mov ebx,-1
line 51:   mov eax,1
line 52:   int 0x80
line 53:
line 54:
line 55:
line 56:
line 57:  _start:
line 58:
line 59:   mov eax,0 ;used for scasb
line 60:   mov ecx,0 ;will be the length value of string,init to  0
line 61:   mov edi,string    ;if  given null will produce an exception-- 
which I handle
line 62:   cld
line 63:   call get_len
line 64:   cmp eax,0
line 65:           js error
line 66:
line 67:
line 68:
line 69:  write:
line 70:
line 71:           or ecx,ecx       ;check if string was null
line 72:   jz exit          ; could implement exception handler
line 73:   mov  edx,ecx     ;length of buffer
line 74:   mov  eax,4  ;sys_write
line 75:   mov  ebx,1       ;file descriptor
line 76:   mov  ecx,string  ;buffer
line 77:
line 78:   int  0x80
line 79:
line 80:
line 81:
line 82:
line 83:  _test:
line 84:           mov edi,[msg]
line 85:           push edi
line 86:
line 87:           mov  edx,4         ;length of buffer
line 88:   mov  eax,4    ;sys_write
line 89:   mov  ebx,1         ;file descriptor
line 90:   mov  ecx,esp       ;buffer
line 91:   add  dword [ecx],4 ;ecx should be 'e'
line 92:           int  0x80
line 93:
line 94:   ;will print 'ebc' on stdout
line 95:
line 96:
line 97:  exit:
line 98:
line 99:   mov ebx,eax
line 100:   mov eax,1
line 101:   int 0x80

_________________________________________________________________

_________________________________________________________________
Enjoy MSN 8 patented spam control and more with MSN 8 Dial-up Internet 
Service.  Try it FREE for one month!   http://join.msn.com/?page=dept/dialup


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2003-11-20 21:52 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-10-23 12:03 confused asm newbie Jason Roberts
2003-10-23 12:53 ` Frederic Marmond
2003-10-23 14:38 ` willy meier
2003-10-23 15:50 ` Philip Jacob Smith
2003-11-01 17:07   ` GRUB sample kernel question ram
2003-11-01 21:24     ` Alexander Jänicke
2003-11-01 22:36       ` ram
2003-11-17 11:47   ` confused asm newbie b klein
2003-11-17 12:29     ` Frederic Marmond
2003-11-17 13:18       ` b klein
2003-11-17 13:36         ` Frederic Marmond
2003-11-18  2:51     ` Philip Jacob Smith
2003-11-20 21:52       ` b klein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).