Author Topic: how to use 'lea' in NASM?  (Read 17047 times)

nobody

  • Guest
how to use 'lea' in NASM?
« on: February 22, 2009, 01:59:12 PM »
Hi!
when I use 'lea' in NASM
------------------------------
Some of my tests:

;file name:test.asm
[bits 16]
org 0100h
jmp main
[section .text]
main:
   lea ax,buf
   jmp $
[section .data]
buf db 'hello world$'
------------------------------
assemble: nasmw test.asm -o test.com

result is here:
test:7: error: invalid combination of opcode and operands

Could you help me?

nobody

  • Guest
Re: how to use 'lea' in NASM?
« Reply #1 on: February 22, 2009, 02:17:49 PM »
I found i can use 'mov' inplace 'lea',am I right? Thank you!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2437
  • Country: us
Re: how to use 'lea' in NASM?
« Reply #2 on: February 22, 2009, 07:32:12 PM »
Yeah, sorta...

http://home.myfairpoint.net/fbkotler/nasmdocc.html#section-A.4.135

You need "lea" (syntax: lea reg, [something]) for an address *calculation*. "lea eax, [buf]" says, "tell me the address of the variable whose address is buf." Kinda overkill. Masm users like it 'cause they don't have to write "offset" that way :) If you want the address of, say [ebp + 8] (sometimes you want contents, sometimes you want address), that's where "lea" comes in.

Since "lea" just does arithmetic - doesn't actually access any memory, you can use it for other calculations, too - must have the "form" of a valid effective address...

atoi:
    mov edx, [esp + 4]  ; pointer to string
    xor eax, eax        ; clear "result"
.top:
    movzx ecx, byte [edx]
    inc edx
    cmp ecx, byte '0'
    jb .done
    cmp ecx, byte '9'
    ja .done

; we have a valid character - multiply
    ; result-so-far by 10, subtract '0'
    ; from the character to convert it to
    ; a number, and add it to result.

lea eax, [eax + eax  *  4]
    lea eax, [eax * 2 + ecx  -  48]

jmp short .top
.done
    ret

Sort of a clever one a C compiler taught me...

Best,
Frank

nobody

  • Guest
Re: how to use 'lea' in NASM?
« Reply #3 on: February 24, 2009, 07:59:26 AM »
thanks a lot