Author Topic: How to set a register zero by using LEA instruction  (Read 370 times)

ElNino1511

  • Regular Member
  • *
  • Posts: 3
How to set a register zero by using LEA instruction
« on: January 27, 2021, 02:59:55 AM »
This is my first post here and go easy on me if I appear too noob.

I know that:

Code: [Select]
lea ebx, [0] ;sets ebx to 0
but the above piece of code works only with NASM but not with MASM32. MASM32 says "error A2070: invalid instruction operands".


What is the reason for this?


This is my full code:

In MASM32:
Code: [Select]
.386
.model flat, stdcall
option casemap : none
   
.code
start:
       
    lea ebx, [0]
   
    ret
    end start


In NASM:
Code: [Select]
%include "io.inc"

section .text
global CMAIN
CMAIN:
       
    lea ebx, [0]
    ret




The final question is "How do I make a register zero using LEA with MASM32"? I know that xor, mov and add can be used to set registers zero. I want to know specifically with LEA. Thanks for your reply.

Vortex

  • Member
  • *****
  • Posts: 2530
Re: How to set a register zero by using LEA instruction
« Reply #1 on: January 27, 2021, 04:13:51 AM »
Hi ElNino1511,

Welcome to the Masm Forum.

I would suggest you to use the statement xor ebx,ebx to set ebx to 0. Doing lea ebx,[0] has no meaning as you try to set the effective address as 0. NULL pointer can be dangerous as you know.

ElNino1511

  • Regular Member
  • *
  • Posts: 3
Re: How to set a register zero by using LEA instruction
« Reply #2 on: January 27, 2021, 04:29:18 AM »
Thanks for your response but I want to know why MASM32 doesn't assemble it while NASM assembles the program happily.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8236
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: How to set a register zero by using LEA instruction
« Reply #3 on: January 27, 2021, 05:05:19 AM »


What you are not telling us is what is in io.inc and what the NASM notation of square brackets is doing with an immediate value of 0.

If this builds in NASM, disassemble it into bare mnemonics and post the result. That will answer your question.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

HSE

  • Member
  • *****
  • Posts: 1617
  • <AMD>< 7-32>
Re: How to set a register zero by using LEA instruction
« Reply #4 on: January 27, 2021, 05:49:57 AM »
Following Hutch's is pretty easy  :biggrin:

MASM have no problem with non sense code, just prevention:
Code: [Select]
.386
.model flat, stdcall
option casemap : none
   
.code
start:
    db 8Dh,1Dh,0,0,0,0
    ret
    end start

Code: [Select]
CPU Disasm
Address   Hex dump          Command             
00401000  /$  8D1D 00000000 LEA EBX,[0]
00401006  \.  C3            RET
io.inc have no porpouse here.

mineiro

  • Member
  • ****
  • Posts: 684
Re: How to set a register zero by using LEA instruction
« Reply #5 on: January 27, 2021, 06:12:18 AM »
if you know program imagebase (startpoint) you can do that like

Code: [Select]
include masm32rt.inc
imagebase equ 401000h

.code
start:
lea eax,offset start - imagebase ;8d0500000000h
lea ebx,offset start - imagebase ;8d1d00000000h
lea ecx,offset start - imagebase ;8d0d00000000h
lea edx,offset start - imagebase ;8d1500000000h

lea eax,word ptr 0
lea ebx,word ptr 0
lea ecx,word ptr 0
lea edx,word ptr 0

ret
end start
You can create a macro with opcodes.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

ElNino1511

  • Regular Member
  • *
  • Posts: 3
Re: How to set a register zero by using LEA instruction
« Reply #6 on: January 27, 2021, 06:28:21 AM »
Thank you very much for your replies. They answered my question.  :biggrin:


jj2007

  • Member
  • *****
  • Posts: 11310
  • Assembler is fun ;-)
    • MasmBasic
Re: How to set a register zero by using LEA instruction
« Reply #7 on: January 27, 2021, 06:33:28 AM »
Thanks for your response but I want to know why MASM32 doesn't assemble it while NASM assembles the program happily.

Probably someone forget to code it (it's an entirely useless instruction) :cool:

Code: [Select]
  8D1D 00000000 lea ebx, [0] ; 6 bytes, flags not changed
  BB 00000000 mov ebx, 0 ; 5 bytes, flags not changed
  33DB xor ebx, ebx ; 2 bytes, zero flag set

Btw UAsm does encode lea ebx, [ 0 ], but AsmC and MASM don't.