WinAsm Studio, The Assembly IDE - Free Downloads, Source Code
Sponsors
Articles
Programming Quick Start
32-bit Assembler is Easy
Porting Iczelion tutorials
What is FASM
What is FASM G
Hard Drive Recovery
Wiring your own LAN
Personal menu
Welcome Guest
User:
Pass:
Register!
Resend Validation Email
 
Forum
Pages (2) [1] 2   ( Go to first unread post )

Asm is tough! Help with pointers, references etc, Working on Ray Seyforth's Book - Chap 6 Exercise 1; not sure how to make arrays or referencing

dskzz
Quote Post


New Member
*

Group: Members
Posts: 2
Member No.: 69367
Joined: 29-July 16


Sponsored Links
Hey redditors at large and asm wizards in particular...
I'm trying to go through Ray Sefarth's noted book 64 Bit Assembly for Linux... I'm on Chapter 6 and trying to do the first exercise, plot the length between two coordinates. Use arrays for the coords.

I'm having trouble figuring out how this reference stuff works. I mean I understand pointers, no problem. But...well...here:

Consider this:

x dd 300

y dd 200

...
xor eax, eax

mov eax, x

sub eax, y

ret

Now I figure that what should happen there is the value of y goes into eax. Y is subtracted from eax. By ret eax should hold a value of 100. That's what my second book, "The Art of Assembly Language" says I should do.

But that's not what happens. According to the Sefarth book I need to do mov eax, [x]

Otherwise the address of x goes into eax. But still confused, doing a debug I don't get the address of x in eax or the value. I get

(gdb) p $eax

$5 = 134518324

WTF is that crazy number?! But then I do

x $eax and I get the address
(gdb) x $eax

0x8049634: 0x000000c8


Reading the "The Art" book imlies that p $eax shows me an offset? But that's a huge number for an offset!

Messing around I try lea eax, x. This is invalid according to yasm. I try lea eax, [x] - I get the same as mov eax, x Is that right? So what's the difference?!

OK, now on to my real question, about the exercise... and let me apologize if maybe this isn't the right forum for this. But redditors are funny and stack exchange is full of jerks who love to focus on side points not the main question (they'd be like uh don't use move use lea, noob) so, there you go you get the post!

Here's my code:
http://pastebin.com/kYC5epN0

The key point is that I am using the array. I move pt_a and pt_b to eax and ebx. I need the memory addresses in there because I want to inc to later get the next memory address. But when I try to sub it doesn't work. What I want is basically to do
sub [ebx], [eax] but that sure doesn't work.

So - questions:

1. Why does "The Art of Assembly Language" say I can use mov eax, var then sub eax, var2 and get the right answer but practice and Sefarth say I need to mov eax, [var]?

2. What the heck is that big number? Does lea eax, [var] == mov eax, var and if so what's the point of lea? And if that crazy number is an offset, then how do I get the REAL address into eax if I need it?

3. I want to do basically sub [eax], [ebx] why can't I? Do I need to do more register gymnastics to make that work?!

4. Oh and I'm trying to define an array; not sure why it doesn't seem to work. I tried dq db dd db...none seem to do the job, and none seem to work with inc eax.

These referencing questions are driving me nuts! Plus I can't figure out how to google this sort of stuff - so would love some advice.







PMEmail Poster
Top
shoorick
Quote Post


Extremely Active Member
******

Group: Admins
Posts: 2331
Member No.: 160
Joined: 22-June 04


i'm not fluent in nasm... but there are some notes on your code:
CODE
pt_a  dd  200, 300
pt_b  dd  550, 350

main:
; loading addresses of arrays
mov eax, dword pt_a  ; eax-->200 or eax-->pt_a
mov ebx, dword pt_b  ; ebx-->550 or ebx-->pt_b

; this gets me my delta x
; sub ebx, [eax]

; here you substract 200 from pointer to 550,
; and ebx now is a pointer to somewhere unknown to us
; that's why you may get a "crasy number"

; if you would like to get in ebx 550-200, then you should use that:

mov ebx,[ebx]; ebx = 550
sub  ebx,[eax]; ebx = ebx - 200

         
mov [delta_x], ebx; there is no "move" command
; xor ebx, ebx - not necesary here

; this moves array to the next set of points
; inc eax - next byte, not dword!!!
; to get next dword you must use this:
add eax,4; eax-->300
mov ebx, pt_b; ebx-->550
; or this:
lea ebx,[ebx + 4]; ebx-->350
; inc ebx - same here

; this gets me my delta y
; sub ebx, eax - here you are subtracting ponter from pointer
; i can suppose the result will be 8;)
; you should use commands in same way:
mov ebx,[ebx]; ebx=350
sub ebx,[eax]; ebx=350-300
mov [delta_y], ebx

;this squares delta x, moves to hypot
mov eax, [delta_x]
mul eax
mov [hypot], eax

;this squares delta y, adds to hypot
mov eax, [delta_y]
mul eax
add eax, [hypot]

; now eax should have the a^2 + b^2
ret

i'm not sure which AOA do you read, but AFAIK it was based on masm. one of the main differences between assemblers are exactly different way of designation values in memory and pointers. as masm/fasm example:
QUOTE
What are the differences between FASM and MASM?

The main difference between FASM and MASM, is the addressing syntax:

Loading an address:

● Equivalent syntax in MASM: mov eax,offset memvar
● Equivalent syntax in FASM: mov eax,memvar

Loading a value:

● Equivalent syntax in MASM: mov eax,memvar
● Equivalent syntax in FASM: mov eax,[memvar]


also: lea is better than mov only when address calculation is used.
say, let's ebx points to structure, where with offset 64 starts an array of dwords, and ecx has number of the dword in it which we need, so, we may load that address into edi with:
lea edi,[ebx+64+ecx*4] ; load memory address into edi
(hard to do the same with single "mov")

instead, if you need the value exactly, not address, you may get it with mov with same address calculation:
mov eax,[ebx + 64 + ecx*4] ; load value from memory to eax

QUOTE
2. What the heck is that big number? Does lea eax, [var] == mov eax, var and if so what's the point of lea? And if that crazy number is an offset, then how do I get the REAL address into eax if I need it?


the real adresses can be strange big numbers, and you may be not able to predict them ;) and you do not need to do that. use labels ;)

resulting executable will contain table of relocations in addition to the original binary, so program addresses will be properly adjusted by the system after loading before start.

This post has been edited by shoorick on Jul 29 2016, 05:17 PM
PMEmail PosterUsers Website
Top
shoorick
Quote Post


Extremely Active Member
******

Group: Admins
Posts: 2331
Member No.: 160
Joined: 22-June 04


to clarify the asm i would suggest you another more clear variant:
CODE

pt_a  dd  200, 300
pt_b  dd  550, 350

mov ebx,pt_a
mov eax,[ebx + pt_b - pt_a]; eax=550
sub eax,[ebx]; eax=550-200
mul eax; eax = (550-200)^2
mov ecx,eax;
lea ebx,[ebx + 4]; shift pointer to next dword
mov eax,[ebx + pt_b - pt_a]; eax=350
sub eax,[ebx]; eax=350-300
mul eax; eax = (350-300)^2
add eax,ecx; eax = (350-300)^2+(550-200)^2
xor edx,edx; sometimes edx mean high dword of 64-bit return in 32-bit system...


or this one:
CODE

pt_a  dd  200, 300
pt_b  dd  550, 350

mov ebx,pt_a; pointer to A
mov ecx,pt_b; pointer to B

mov eax,[ecx]
sub eax,[ebx]
mul eax; eax=(xb-xa)^2
push eax

mov eax,[ecx + 4]
sub  eax,[ebx + 4]
mul eax; eax=(yb-ya)^2
pop edx
add eax,edx; eax=(yb-ya)^2+(xb-xa)^2
xor edx,edx


PMEmail PosterUsers Website
Top
dskzz
Quote Post


New Member
*

Group: Members
Posts: 2
Member No.: 69367
Joined: 29-July 16


Wow thanks for the great responses! That stuff really helps clear up some of the things that have been causing me a non stop headache! I'm going to absorb and redo then hopefully come back and thank you all again.

FYI it was this AoA
http://flint.cs.yale.edu/cs422/doc/art-of-asm/pdf/
PMEmail Poster
Top
dskzz
Quote Post


New Member
*

Group: Members
Posts: 2
Member No.: 69367
Joined: 29-July 16


Alright I finally got it working! Learned a lot out of this one.

My code isn't as tight as yours, but reading your code I see the clear benefits of how you did things.

V Cool. For a while delta_x kept getting overwritten. I realized after some debugging it was declaring as a word and delta_y was overwriting part of the value. These data sizes - not something you need to deal with with high level stuff. Think I'm getting it strait.

I'll sure be back over the next week as I digest the rest of the book. Hope this forum is just as helpful! Next task is figuring out how to square root the hypotenuse

PS read some assembly answers on stackExchange and yeah just as obnoxious as I figured, liberally sprinkled with RTFM commentary

Here's my code.

CODE

segment .data
pt_a  dd 200,300
pt_b  dd 550,350
delta_x dd 0
delta_y dd 0
hypot_a dd 0
hypot_b dd 0
hypot_c  dd 0


 segment .text
 global main
 
 main:
mov eax,  [pt_a]; this moves the address of pt_a to eax; GOOD = 200
mov ebx,  [pt_b]; this moves the address of pt_b to ebx; GOOD = 550
sub ebx, eax  ; GOOD ebx = 350.
mov [delta_x], ebx; save it; GOOD delta_x =350; 0x8049654  ; We are good up to here.
mov eax, [pt_a+4]
mov ebx, [pt_b+4]
sub  ebx, eax
mov [delta_y], ebx; OK this is all good!;Get the a^2 and b^2
mov eax, [delta_x]
mul eax
mov [hypot_a], eax
mov eax, [delta_y]
mul eax
add eax, [hypot_a]
mov [hypot_c], eax
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

Topic Options Pages (2) [1] 2  Reply to this topicStart new topicStart Poll

 

Sponsors
Computer Science

Internet
C/C++
Hardware & PC maintenance

HiEditor

General Discussions
Suggestions/Bug Reports
WinAsm Studio

General Discussions
Suggestions/Bug Reports
WinAsm Studio FAQ
Multilingual User Interface
Add-Ins
Assembly Programming

Main
Newbies
Projects
Custom Controls
Snippets
Announcements & Rules

Announcements

General

Online Degrees - Distance Learning
The Heap
Russian
Google