NASM Forum > Programming with NASM

How are you multiplying by 3

(1/3) > >>

gygzkunw:

--- Code: ---SECTION .bss
BUFFLEN equ 16 ; ----> Bufflen = 16
Buff: resb BUFFLEN ; ----> Buff will read 16 bytes from the buffer

SECTION .data ; Section containing initialised data

HexStr: db " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",10
HEXLEN equ $-HexStr ; ---->length of Hexstr

Digits: db "0123456789ABCDEF"

SECTION .text ; Section containing code

global _start ; Linker needs this to find the entry point!

_start:
nop ; This no-op keeps gdb happy...

; Read a buffer full of text from stdin:
Read:
mov eax,3 ; ----> syscall read
mov ebx,0 ; ----> read from standard input (keyboard)
mov ecx,Buff ; ----> where the string read is stored
mov edx,BUFFLEN ; ----> how many bytes is supposed to read
int 80h ; ----> sys call
mov ebp,eax ; ----> copy the number that where actually read into ebp
cmp eax,0 ; ----> sub 0 from eax and set the appropriate flag? (if user enters 0 shouldn't the program end?)
je Done ; ----> if the zero flag is set move tone the label done

; Set up the registers for the process buffer step:
mov esi,Buff ; ----> copy the content of Buff into esi
mov edi,HexStr ; ----> copy hexstr into edi
xor ecx,ecx ; ----> Clears ecx

; Go through the buffer and convert binary values to hex digits:
Scan:
s xor eax,eax ; ----> clears eax

; Here we calculate the offset into the line string, which is ecx X 3
mov edx,ecx ; ----> copy ecx which is 0 into edx
lea edx,[edx*2+edx]     ; ----> This is where i am stuck. How are you able to multiply by 3 over here?
LEA = [BASE +( INDEX * SCALE) + DISP)]
      if edx is 0, how are we able to multiply by 3
--- End code ---

I am not understand how the author is using the lea instruction
The code is from Assembly Language Step-By-Step by Jeff Duntemann

Thank you

Frank Kotler:
Well... it's a "trick" using lea (load effective address) to "load"  edx with edx plus edx times two... As you observe, zero times three is zero. You don't show it, but we then increment edx.. and ,,,

I've never read "Step by Step"... I ASSume Jeff explains this...?

Best,
Frank




gygzkunw:

--- Quote from: Frank Kotler on February 22, 2021, 03:07:34 AM ---Well... it's a "trick" using lea (load effective address) to "load"  edx with edx plus edx times two... As you observe, zero times three is zero. You don't show it, but we then increment edx.. and ,,,

I've never read "Step by Step"... I ASSume Jeff explains this...?

Best,
Frank

--- End quote ---
I get its a trick but i don't see where the 3 is coming from...

"Not only is this virtually always faster than shifts combined with adds, it’s also clearer from your source code what sort of calculation is actually being done. The fact that what ends up in EDX may not in fact be the legal address of anything is unimportant.LEA does not try to reference the address it calculates.It does the math on the stuff inside the brackets and drops it into the destination operand. Job over. Memory is not touched, and the flags are not affected.Of  course,  you’re  limited  to  what  calculations  can  be  done  on  effective addresses; but right off the top, you can multiply any GP register by 2, 3, 4, 5, 8,and 9, while tossing in a constant too! It’s not arbitrary math, but multiplying by  2,  3,  4,  5,  8,  and  9  comes  up  regularly  in  assembly  work,  and  you  can combine LEA with shifts and adds to do more complex math and ‘‘fill in the holes.’’ You can also use multiple LEA instructions in a row."

Is there a book you like to recommend? You dont seem to like the book...

Frank Kotler:
edx plus two times edx  equals three times edx. I don't think I understand the question.

I have not read Jeff Duntemann's book. I did not mean to give the impression I didn't like it! It has an excellent reputation. I don't have another suggestion. Dr. Paul Carter's Tutorial, perhaps?

Best,
Frank

gygzkunw:
edx+2 * edx = 3edx-> This is my question, how is this possible?


But earlier mov edx,ecx -> 0 was moved into edx

edx(0)+ 2*edx(0) =should equal to 0, right?
Do you understand what I am saying here?

Navigation

[0] Message Index

[#] Next page

Go to full version