Re: Kind of new: function implementation questions, MASM

From: Tim Roberts (spamtrap_at_crayne.org)
Date: 09/24/04


Date: Fri, 24 Sep 2004 03:35:06 +0000 (UTC)

spamtrap@crayne.org (Ross A. Finlayson) wrote:
>
>I start writing a NASM implementation of my toy function. I make some
>progress, but not much.
>
>I don't yet understand the stack or the data segment.

In 32-bit programming, you should simply ignore these concepts. You may
pretend that all 4GB of address space is simply available to you. Every
byte of memory is referred to by its 32-bit linear address.

>I think on entry to the function I should establish the stack frame.
>
>enter 12, 0
>
>That 12 is for 12 bytes of input variables, a unsigned integer and two
>void pointers. I think it's faster to use the boilerplate prolog.

It's actually faster to do the "enter" instruction by hand:
        push ebp
        mov ebp, esp
        sub esp, 12

>I define the variables as so:
>
>%define inputlength [ebp+12]
>%define input [ebp+16]
>%define output [ebp+20]
>
>I'm not sure, but in reading the NASM documentation for the enter
>instruction, I think those maybe should be
>
>%define inputlength_s [ebp-4]
>%define input_s [ebp-8]
>%define output_s [ebp-12]

Correct. Notice, in the sequence I posted above, that ebp will point to
just beyond the newly allocated 12 bytes of space.

>I'm unclear on what is the stack and how it is organized and data
>stored therein.

A stack is just a piece of memory. "esp" happens to point to the top (the
lowest address) byte of that memory. The only fancy thing about the "push"
instruction is that it automatically decrements the pointer before it
writes. Thus, ignoring exceptions, this:
        push eax
is exactly the same as:
        sub esp, 4
        mov [esp], eax

It is a handy place to put temporary variables, because the space goes away
when your function ends. That happens because you will restore the
original value of esp at the end. The stuff you stored on the stack is
still there, but it will get overwritten by the next functions call.

>I define a register variable, these definitions are only preprocessor
>definitions.
>
>%define currentinput_r ecx
>
>I use _s to indicate it's a stack offset definition, and _r to
>indicate it's a register definition.

Well, this is a matter of personal preference, but most x86 assembly
programmers do not find the concept of register equates to be useful,
primarily because there are so few registers available.

>Then, I have not understanding of the data segment. Basically I want
>the data segment to be a block of memory that I can access.

Right. There are two concepts at work here with the same name. Your
program can have a .data segment (or "section", in PE terms), which
contains the pieces of global memory your program will user. In the
processor, the term "data segment" refers to the memory pointed to by the
DS register. In 32-bit programming, as I said, you can ignore DS; it
points to the entire linear address space.

>SECTION .data align = 1

Don't think you want to specify "align = 1". You probably just want the
default, so you get things aligned neatly.

>db 0x34
>db 0x12 ; little endian 0x1234
>db 0x78
>db 0x56 ; little endian 0x5678
>...
>
>Then I use ds as the offset pointer to the data segment?

No. To USE this memory, you must assign a name to it:

one db 0x34
        db 0x12
two db 0x78
        db 0x56

Now you can do "mov al, [one]" to fetch that first byte.

If you really want words, you can also say:

one dw 0x1234
two dw 0x5678

>I generate
>those codes from other de-bugged files so changes are not painful.
>What's in the extra segment es?

In 32-bit programming, it is the exact same thing as ds. It starts at 0
and contains all of memory. Ignore it.

>Then, I adjust the data pointer by the carry flag. Then, move the
>data pointer contents to a register.
>%define data_s ds ; data stack pointer
>%define currentdecision_r dx ; 16-bit general purpose register

No. You do not change ds. All you use are the offsets of the symbols you
want. However, I can't figure out here what address you're trying to
fetch.

>I think because I don't use string instructions that I can use edi and
>esi if I need more registers.

Well, if you need string instructions, you either arrange things so that
esi and edi are free, or you push and pop them on the stack while you do
the string operation.

>Another thing is that I think using test allows more o-o-o
>(out-of-order) execution, because there would be no dependency if the
>internal registers have their own EFLAGS. I've just read to avoid
>branches wherever reasonable, in, trying to understand what
>"reasonable" is.
>
>I don't know if the prefetch will be any good because the memory is
>accessed perhaps too shortly after the prefetch.

Honestly, Ross, the kind of optimization you're talking about here is Step
37. You're on Step 6. Get the thing working first, THEN measure to see if
it is fast enough, THEN figure out where it is spending its time, THEN
optimize.

-- 
- Tim Roberts, timr@probo.com
  Providenza & Boekelheide, Inc.


Relevant Pages

  • Re: Calculating checksums...
    ... - it's a word memory reference using a register address ... lods - it's the lods instruction ... parens are a memory reference. ... programming and why, with the end of the 68k, teaching assembly ...
    (alt.lang.asm)
  • Re: Cell Architecture Explained (MASSIVE AMOUNT OF INFO)
    ... >Programming Itanic was a picnic compared to programming this thing; ... >So in return for giving up cache, your code has to manually move data ... >to/from memory. ... low-bandwidth stream register file that faces memory and local ...
    (comp.arch.embedded)
  • Re: Calculating checksums...
    ... A register are flip-flops within the CPU and if you want to give them ... The x86 has three separate address spaces: the memory address ... programming and why, with the end of the 68k, teaching assembly ...
    (alt.lang.asm)
  • Re: can somebody help me with the problem with tasm models
    ... When Intel created the x86 originally, ... registers...now, when addressing memory with something like "", this ... valid...the rest aren't yet wired in and are ignored in memory addressing ... "offset" register, this would give a 20-bit address...if, in time, they ...
    (alt.lang.asm)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... technical definition of a programming language) which in ordinary ... usage has a "wide variety of exact meanings in many walks of life", ... whether some random set of memory cells in a C core image, ... north-west relative to the rest of the Bay Area. ...
    (comp.lang.c)