GIDForums  

Go Back   GIDForums > Computer Programming Forums > Assembly Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #21  
Old 13-Dec-2008, 03:48
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Assembly Tutorial?


ok i finished chapter 3 i am not getting the adressing mode it seems like either using the value of a register directly or using memory adress i just did not get it.
i am also not getting how the command echo$? will dispay the content of register %EBX, so why not %EAX
i am posting my coe to modify this list serach to return the minimum value in the list check it out i can not run it at work so i have to go home to see if it compiles right.
CPP / C++ / C Code:
.section .data
data_items:
.long 3,67,34,222,45,75,34,44,33,22,11,66,0

.section .text
.global_start
_start:

movl $0, %edi 			# initilized my index to 0
movl data_items(, %edi, 4), %eax 	# moving item index 0 to EAX
movl %eax, %ebx 			# initializing EBX to the value of EAX

start_loop:			# loop  tag

cmpl $0, %eax			# check if EAX is 0

je loop_exit			# Goto loop_exit if 0==%EAX

incl %edi				# increment EDI ++
movl data_items(, %edi, 4), %eax	# move item idex 1 to EAX
cmpl %eax, %ebx			# compare %EAX to %EBX

jg star_loop			# jump to start_loop if EAX is greater or equal to %EBX

movl %eax, %ebx			# if not jge then Assign EBX to EAX

jmp start_loop

loop_exit:

movl $1, %eax
int  $0x80
thanks Howard
  #22  
Old 13-Dec-2008, 14:13
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Assembly Tutorial?


Code:
.section .data data_items: .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0 re-1: the data_items is a tag name we call that an 'identifier' , we use 'tags' in html... I think it would also be considered a 'symbol' in assembly. ---- re-2: the last 0 will indicate no more items in the array long type zero is the terminating character. So this is a zero terminated string , the same as a C string. ---- re-3: .section .text "??" ## a 'symbol' to indicate the program section .global _start "??" ## a global scoped symbol named: 'start' ---- re-4: movl data_items(,%edi,4), %eax " here we are asigning the value of the first item in the array type (long, i think the size is 4 byte) which index 0 to register eax" YES! 'data_items' holds and address (like a C array identifier) %edi holds a value representing the indice (value is 0 at this time), the data size must be stated in the 3rd parameter (yes 4 bytes, 32 bits). ---- re-5: start_loop: "??" He's setting a symbol in the program running order. (contains an address) Concept: The program runs from _start till 'nothing else to be done'. (exit) It is arranged in memory in the same order you see here. That 'start_loop' symbol will hold the address of that point in program. Then you will be able refer to it to 'jump' (jmp, je) to later. Think of it as a marker... ---- re-6: cmpl $0, %eax je loop-exit " tag to the code that ends the application" Kinda,,, It says: If (the cmp operation set the %eflags register to indicate equal) { jump to the address stored in 'loop-exit' (which is just before the exit routine). } ---- re-7: mov data_items(,%edi,4), %eax " loading the next item in the list i hope u can go over this part i do understand what it is doing but the syntax part i wanna know more about it" yes this is an important concept to grasp especially since your focus is supposed to be toward data structures Note that in: (,%edi,4) there is a first value missing (which is ok) This is basically like pointer arithmetic in C (C++) Think of that as a indice dereferencing statement like: 'data_items[%edi]'. In C++ we would define that 'data structure' like this: int data_items = { 3,67,34,222,45,75, ...} C++ will log the size info automatically so we can use indicing as above. In assembly we must supply all of that info. In chapter 3 he has the section 'Addressing Modes' ... review (again)... Note that what we are seeing here is in x86 syntax (AT&T) Intel syntax does this differently, something like: 'data_items[edi:4]' I think. ---- re -8: cmpl %ebx, %eax "if %ebx>%eax skip a line and read after it else go back to the loop so we will retreive the next item. i wish if u go over the cmpl instruction cuz the book says it compare two integer and discard the result but returns a flag ??" Right, good questons... (You forgot the all-important following jle) We take so much for granted in C when we do: if( ebx > eax ) The 'if' looks for a true or false from the operation which detemines whether it will run or 'jump' past the next line (or block of code). This is how all that really works! cmpl %ebx, %eax jle start_loop { true: 'goto' that 'start_loop' address} OR false: fall through to next line ( replace the maximum) Let's see if I can get this... So, in the book: 'Appendix B. Common x86 Instructions' shows me:
Quote:
cmpl I/R/M, R/M O/S/Z/A/P/C Compares two integers by subtracting the first operand from the second. It discards the results, but sets the flags accordingly.
OK, see the description of the eflags register at the begining of that appendix. It's not very clear how true or false is set but if I take it one step at a time: On the first pass ebx holds 3 , eax holds 67. (trace code with pencil and paper if needed) So cmpl does this: 67 - 3 = 64 looking at the flag descriptions ask yourself: Self? - is overflow set? no - is sign set? no - is zero set? no - is aux carry set? no - is parity set? no - is the carry set? no Ok now look at the jcc description in the same appendix:
Quote:
Jcc destination address O/S/Z/A/C Jumps to the given address if the condition code is true
So it looks at those eflags (OSZAC) and lets see, are any of them true? NO So jle is false , we fall through the jle and exchange the values.... Then we hit the jmp which takes us back up to the 'start_loop:'. (I'm reallly confused now) Ok so now ebx holds the new value 67 and eax comes up with 34: So cmpl does this: 34 - 67 = -33 (you see, result is only needed to set flags) - is overflow set? no - is sign set? yes - is zero set? no - is aux carry set? yes - is parity set? no NOT CHECKED (see reference above) - is the carry set? yes So the jle sees all the trues and so takes us back up to 'start_loop:' eax is NOT movl 'ed to ebx. Does that make sense to you? Explain it to me then! Now, you must be thinking: 'Howard , to know all that flag stuff, you sure are a fart smella'... I would say to you 'whoeva is the smella is mos likely the della!' Seriously: ...realizing why the Sign flag is set is obvious to me but I am learning these things as we speak and writing them down as best I can for now. For this part I have been using 'gdb' to observe these values which is something I have mentioned a few times but haven't gone into. With that I can see all the register values: eax 0x22 34 ecx 0x0 0 edx 0x0 0 ebx 0x43 67 esp 0xbfbfb6a0 0xbfbfb6a0 ebp 0x0 0x0 esi 0x0 0 edi 0x2 2 eip 0x8048091 0x8048091 <start_loop+15> eflags 0x293 [ CF AF SF IF ] Pretty cool huh? Ready to learn that yet , it's easy and really neat. ---- Re-9: "now the last part is to call interruption 80 to exit the application. It's an 'interrupt' which causes an interruption... (picky, picky) ---- Re-10: i also want to know how echo $? will display the %ebx which is holding the max value in the array" It's like the 'int' value which is returned with 'int main()' (to my understanding, this is one byte and so the max value is 255) You can check the return of any process this way in linux. $ is used to indicate a shell 'variable' which is also an 'identifier'. The ? is a special 'environment' variable ,, that is it's one of the many values specified to make your working environment the way it is... type: env ... at the command line... (read the bash prompt howto) Well I hope I got some stuff right anyhow. I'll post and review.... sorry so long getting this done, I am very slow...
Last edited by Howard_L : 13-Dec-2008 at 15:26.
  #23  
Old 13-Dec-2008, 15:38
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Assembly Tutorial?


hi howard
ok the register eflags is kinda out of my league for now but i think i can use properly in boolean comparison actually what i did this morning is to make my last item in the list 10 and i inserted a new item =1 then i modified the code to get the min number
now when i tried to insert an .ascii item into a register and display it than i had a problem
how to use the gdb command to dispay the contenat of all the registers
Last question u think i should move the chap4 or just review the first three
  #24  
Old 13-Dec-2008, 16:39
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Assembly Tutorial?


If you have gdb , you would compile your code with the --gstabs option:
Here is a good tutorial I didn't want to show you earlier because it has hello, but I think you will not even care to look at that part now, you're hooked!:
database.sarang.net/study/linux/asm/linux-asm.txt
Learn the techniques on his code and then use them on your code. I gotta go.
  #25  
Old 15-Dec-2008, 01:55
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Assembly Tutorial?


I am kinda lost, tht tutorial that u gave kinda lost me so this is what i need to know how to declar these variable in assembly?
int len=125;
cout<<len;
string str="Heikel"
cout<<str;
bool found=true;
cout<<found;
char ch1,ch2;
ch1='A';ch2='B';
cout<<ch1<<ch2;

i want to see how can i add two int
int a,b;
a=100;b=50;
cout<<a+b;

thanks Howard.
  #26  
Old 15-Dec-2008, 13:55
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Assembly Tutorial?


No offense but that is ridiculous... I sense frustration : )
I can't just write that for you. for two reasons. -1- You wouldn't learn anything ..and -2- I don't know how!!!
To print a numeric value you have to convert it into an ascii string?
I haven't even tried to do that yet... and a string class? way outa my league!
You must keep in mind that in assembly we work with the basic building blocks.
It takes a heck of a lot of building blocks to make 'cout<<len' possible!
But I am willing to work along with you to learn how to put some things together.
The task is overwhelming to me too but I find I am able to get there one step at a time.
That being said , NO you should not move on to chapter 4...

...did you fix your attempt at finding a minimum value (post #21) I'm guessing not.
I think it would be to your benefit to learn to step through this with gdb and find the problem.
I had thought that tutorial might guide and prompt you into it, but apparently that was not to be., woe....
Here's a short howto use gdb to solve all your problems (except butt itch):
Code:
## Referring back to you post #21 'minimum value ---NOT' problem: ## First of all a couple of things got fouled up in your posting: line 6: .global_start ##should be .global _start line 23: star_loop: ##should be start_loop: ## To use gdb to trace through your program you need to compile it ## with the '-gstabs' option to add debugging information to the binary. as -gstabs 081212_zatora1.s -o 081212_zatora1.o ## link it as normal ld 081212_zatora1.o -o 081212_zatora1 ## Then run it in the gdb debugger like this: gdb ./081212_zatora1 ## So gdb cranks up with the program in it. ## Then set a break point to stop the program from running straight through: ## (the * means an address +5 means up 5 bytes from there. (remember our ## code goes into memory, the _start symbol represents an address )) (gdb) break *_start+u Breakpoint 1 at 0x8048079: file 081212_zatora1.s, line 10. ## Then 'run' the program which stops at the breakpoint , showing the next line to be executed: (gdb) run Starting program: /mnt/e/forums/081212_zatora1 Breakpoint 1, _start () at 081212_zatora1.s:10 10 movl data_items(, %edi, 4), %eax # moving item index 0 to EAX Current language: auto; currently asm ## Then we begin to 'step' through the program: (gdb) step _start () at 081212_zatora1.s:11 11 movl %eax, %ebx # initializing EBX to the value of EAX ## Line 10 has been executed. Line 11 is shown and will be next. ## Note: A press of [ENTER] will repeat the last gdb command (which was 'step') (gdb) start_loop () at 081212_zatora1.s:15 15 cmpl $0, %eax # check if EAX is 0 ## Note that 's' can be used as a shortcut to 'step'. This is true for most. ## ok so to get a feel just step through the code until it exits thusly: (gdb) loop_exit () at 081212_zatora1.s:32 32 int $0x80 (gdb) Program exited with code 0336. ## 336? there is no 336 on the list! ## Oh , the leading 0 (zero) means it's the value is shown in 'octal' (base 8) ## ok so 6 + (3*8) + (3*64) = 6 + 24 + 192 = 222 ... still the maximum value! ## So it still finds the maximum instead of the minimum. So what's going wrong? ## run through it again but pay special attention this section: (gdb) start_loop () at 081212_zatora1.s:21 21 cmpl %eax, %ebx # compare %EAX to %EBX (gdb) start_loop () at 081212_zatora1.s:23 23 jg start_loop # jump to start_loop if EAX is greater or equal to %EBX ## now stop right there and look at the registers with: (gdb) info register eax 0x43 67 ecx 0x0 0 edx 0x0 0 ebx 0x3 3 esp 0xbfaf5db0 0xbfaf5db0 ebp 0x0 0x0 esi 0x0 0 edi 0x1 1 eip 0x8048091 0x8048091 <start_loop+15> eflags 0x287 [ CF PF SF IF ] cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x0 0 ## Keep in mind line 21 has just been done and 22 is next ## The compl has been done and eflags has been set according to the result. ## compl %eax, %ebx -does this ebx - eax == 3 - 67 == -64 ## so the Carry , Parity , and Sign flags are set (IF is always set... ) ## now a look at the reference shows jg checks truth of positive result ## the sign bit is set so jg is going to say 'false' ## so the jump up will not occur and we fall through and value will be changed. ## so let's see it happen! ## Note: The uparrow keys will scroll though previous commands. (gdb) s start_loop () at 081212_zatora1.s:25 25 movl %eax, %ebx ## Which way did we go? The right way... ## BUT the WRONG way if we are looking for the minimum value!!! ## So that is where the problem is. So get in there and FIX IT ! ## pretty cool huh. ## Type 'help' for a full list of gdb command topics to look into and try out. ## The end
C'mon give it a try!
Last edited by Howard_L : 15-Dec-2008 at 14:51.
  #27  
Old 15-Dec-2008, 15:42
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Assembly Tutorial?


Hi, i don't believe that i am frustrated if that what u meant learning is a long journey
i will tell exactly where i am at.
i know that we need to move data from memory to register or even from register to register
so my goal was to get familiar to more commands and also try to get use to manipulate data like i know how to use long type variable and i don't how to use integer or char or double but to cheer you up a little i fixed my star_globl and i did put in the right code to get the minimum value in the array i even changed the last value to 1 so i had to change eax to 1 to enable the compiler to know that and it did work. now i am practicing playing with moving data to registers like this code which did work too
CPP / C++ / C Code:
.section .data
item_data:# var type long =12
.long 12
item:#var type long =20
.long 20
.section .text
.globl _start
                                        
_start: #beg of compiling
 movl $1, %eax   #system call ()      
movl item_data, %ecx # i moved item_dat into ecx
movl item,%ebx	# i moved item into ebx
addl %ebx,%ecx #i added ebx+ecx into ecx
movl %ecx,%ebx #i moved ecx to ebx      
 int $0x80   
now i will submit the part that i did not get in the tutorial that u sent me its link and trust me i did not gave up on it, so therefore i will post the code and what part i did not get in it
CPP / C++ / C Code:
	.section .data
hello:	
	.ascii 	"Hello, world!\n"# here it is a string or char[] array
hello_len:
	.long 	. - hello#here he is giving the size of hello which is a word
########################################################################
	.section .text
	.globl _start
	
_start:
	## display string using write () system call
	xorl %ebx, %ebx		# %ebx = 0# here where i am confused why not movl    #$0,%ebx cuz he said he wanted bx to be ==0
	movl $4, %eax		# write () system call (i am not sure what is that really)
	xorl %ebx, %ebx		# %ebx = 0 # again why
	incl %ebx		# %ebx = 1, fd = stdout # %ebx++;
	leal hello, %ecx	# %ecx ---> hello# i did not understand this command
	movl hello_len, %edx	# %edx = count# here where it seems not clear
	int $0x80		# execute write () system call# why now he is calling the int 80
	
	## terminate program via _exit () system call 
	xorl %eax, %eax		# %eax = 0
	incl %eax		# %eax = 1 system call _exit ()
	xorl %ebx, %ebx		# %ebx = 0 normal program return code
	int $0x80		# execute system call _exit ()
so i have to finish reading your post and i will ask you this why this program display "Hello World" without typing echo $? ( i hope you will inderstand that this is my 4th day with assembly) so again i need to lear hoe to use dgb then i hope that you can comment the "Hello Word" code. thanks Howard
  #28  
Old 15-Dec-2008, 15:54
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Assembly Tutorial?


i tried the gdb this is what i got
CPP / C++ / C Code:
heikel@heikel-desktop:~$ cd Assembly
heikel@heikel-desktop:~/Assembly$ as -gstabs beg.s -o beg.o
heikel@heikel-desktop:~/Assembly$ ld beg.o -o geb
heikel@heikel-desktop:~/Assembly$ ./beg
heikel@heikel-desktop:~/Assembly$ echo $?
32
heikel@heikel-desktop:~/Assembly$ as -gstabs beg.s -o beg.o
heikel@heikel-desktop:~/Assembly$ ld beg.o -o geb
heikel@heikel-desktop:~/Assembly$ gdb ./beg
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) gdb ./beg
Undefined command: "gdb".  Try "help".
(gdb) 

so may be u can tell me how to get it right this time lol?
  #29  
Old 15-Dec-2008, 16:25
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Assembly Tutorial?


Quote:
i hope you will inderstand that this is my 4th day
Id and I'm glad to see your still at it.
Too often do we see 'I Need blah blah...' postings around here (you will see)where people just want their homework done for them without doing any of the work.
I hope you will stick around and help others.
Code:
(gdb) gdb ./beg ## No, you don't do that within gdb. ## That's the command line to start gdb with the program loaded which you already did. ## Next you set the breakpoint and... wow I just looked back up there and it says: (gdb) break *_start+u ## That is WRONG !!! I don't know what happended there... it should be: (gdb) break *_start+5 ( and my edit time is way past ,,, what a 'duh',,, sorry) ## Then step,,, (just follow the steps)

re: xorl %ebx, %ebx #why not: movl $0, %ebx
Yes he's using xor to zero out the register. (look up 'xor' in bitwise operators to see what it does)
I believe it is considered safer that 'movl $0, %ebx'.
You do realize that when we refer to %eax (and any register id with the e prefix) we are referring to the 'extended' ax register. It is 32 bits. the 'ax' register is the lowest 16 bits of that and the 'al' register is the lowest 8 bits of ax,,, and the 'l' on xorl means that it xor's all 32 bits. There is a chance that movl $0, %eax may only set the lowest 8 or 16 bits to 0. We learn not to assume things like that may not always work as expected.

re: movl $4, %eax # write () system call (i am not sure what is that really)
Yes it's a linux operation code number for the 'write' function.
I posted a link to a site with a linux opcode reference: cin.ufpe.br/~if817/arquivos/asmtut/index.html

Type 'man 2 write' to get the man page for it. There you will see that it is from the unistd.h library and the the prototype (interface) is:
ssize_t write(int fd, const void *buf, size_t count);
Those parameters are given in ebx, ecx, and edx
Look up leal in appendix. basically it's transferring the 'address of' (pointer to) the hello string to %ecx.
The program as posted runs OK for me.
Last edited by Howard_L : 15-Dec-2008 at 17:53.
  #30  
Old 17-Dec-2008, 19:54
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: Assembly Tutorial?


Hi, ok i tried the gdb again this what i wrote in the source code
CPP / C++ / C Code:
.section .data
item_data:# var type long =12
.int 12
item:#var type long =20
.int 20
.section .text
.globl _start
                                        
_start: #beg of compiling
 movl $1, %eax   #system call ()      
movl item_data, %ecx # i moved item_dat into ecx
movl item,%ebx	# i moved item into ebx
addl %ebx,%ecx #i added ebx+ecx into ecx
movl %ecx,%ebx #i moved ecx to ebx
          
 int $0x80   
that was a small attempt to manipulate data arround.
when i tried to debug this what i typed and this what i ve got
CPP / C++ / C Code:
heikel@heikel-desktop:~$ cd Assembly
heikel@heikel-desktop:~/Assembly$ as -gstab beg.s -o beg.o
as: option '-gstab' is ambiguous
heikel@heikel-desktop:~/Assembly$ as -gstabs  beg.s -o beg.o
heikel@heikel-desktop:~/Assembly$ ld beg.o -o beg
heikel@heikel-desktop:~/Assembly$ ./beg
heikel@heikel-desktop:~/Assembly$ echo $?
32
heikel@heikel-desktop:~/Assembly$ (gdb) break *_start+5
bash: syntax error near unexpected token `break'
heikel@heikel-desktop:~/Assembly$ as -gstabs  beg.s -o beg.o
heikel@heikel-desktop:~/Assembly$ ld beg.o -o beg
heikel@heikel-desktop:~/Assembly$ (gdb) break *_start+5
bash: syntax error near unexpected token `break'
heikel@heikel-desktop:~/Assembly$ gdb
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
(gdb) break *_start+5
No symbol table is loaded.  Use the "file" command.
(gdb) so i am kinda not getting it specially  that
i am new to lunix and diifeerntcompiler other than microsoft visual c++
now i am reading about registers and the data type associated with them .
i got the part where Eax stands for extended and i remember that it said in the book (http://www.drpaulcarter.com/pcasm/index.php)
that there is no way to access the higher EAX(16 bits) so we kinda still dealing wiith the AX
If u know what these register are designed for a small help will be great and specially the EFLAGS ??? the O C Z .... what are they ? the book explanation does not provide a good example to understand the concept also i am a newbi.
thnak you again
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mixing C and assembly in x86 - Makefile nuances aijazbaig1 Assembly Language 3 23-Apr-2008 09:29
Tutorial: How to Make a Web 2.0-Style Logo PhotoshopTrend Graphics Forum 0 20-Sep-2007 06:57
Assemblers & assembly language BlueFireCO. Assembly Language 2 26-Mar-2007 10:56
Photoshop Tutorial: Make An Inspirational/Mystical Picture ToddSAFM Graphics Forum 9 09-Aug-2005 21:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:18.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.