Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 8th, 2005, 3:19 PM   #1
guzpapir
Newbie
 
Join Date: Aug 2005
Posts: 4
Rep Power: 0 guzpapir is on a distinguished road
Basic examples-what does it means

I started learning Asembler recently,from the book "The art of Assembly Language" and I came to the part with some examples.Well,the problem is that I dont understand some of them.Here are the example proggies:

1.
; Some variables we will access indirectly (using pointers):

dseg            segment para public 'data'

J               word    ?

; Some simple uninitialized array declarations:

ByteAry         byte    4 dup (?)

dseg ends

; The following program demonstrates how to access each of the above
; variables.

cseg            segment para public 'code'
                assume  cs:cseg, ds:dseg

Main            proc
                mov     ax, dseg        ;These statements are provided by
                mov     ds, ax          ; shell.asm to initialize the
                mov     es, ax          ; segment register.

                mov     J, 0
                
; The following code shows how to access elements of the arrays using
; simple 80x86 addressing modes:

                mov     bx, J           ;AL := ByteAry[J]
                mov     al, ByteAry[bx]
My question is how can J which is word can be an element of ByteAry when we determinated earlier that elements of ByteAry will be bytes(ByteAry byte 4 dup (?) ) ?

2.The values of vairable K and J are:
K integer 4
J integer ?


lea     bx, L           ;Point bx at first word in L.
                mov     ax, [bx]        ;Fetch word at L.
                add     ax, 2[bx]       ;Add in word at L+2 (the "1").
                add     ax, 4[bx]       ;Add in word at L+4 (the "2").
                add     ax, 6[bx]       ;Add in word at L+6 (the "3").
                mul     K               ;Compute (0+1+2+3)*123.
                mov     J, ax           ;Save away result in J.

                les     bx, PtrVar2     ;Loads es:di with address of L.
                mov     di, K           ;Loads 4 into di
                mov     ax, es:[bx][di] ;Fetch value of L+4.

I dont understand what does the "1","2","3" means and this part:
mul K ;Compute (0+1+2+3)*123 -isn't the value of K=4 ?




3.
string          byte    "Hello world",0dh,0ah,0

What does odh,oah,0 means?

4.
J               word    0, 0, 0, 0
K               word    1, 2, 3, 4
L               word    5, 6, 7, 8

Ptr1            nWrdPtr ?
Ptr2            nWrdPtr K               ;Initialize with K's address.
Ptr3            fWrdPtr L               ;Initialize with L's segmented adrs
    Add the four words in variables J, K, and L together using pointers to
; these variables:

                mov     bx, Ptr1        ;Get near ptr to J's 1st word.
                mov     si, Ptr2        ;Get near ptr to K's 1st word.
                les     di, Ptr3        ;Get far ptr to L's 1st word.



                mov     ax, ds:[si]     ;Get data at K+0.
                add     ax, es:[di]     ;Add in data at L+0.
                mov     ds:[bx], ax     ;Store result to J+0.

                add     bx, 2           ;Move to J+2.
                add     si, 2           ;Move to K+2.
                add     di, 2           ;Move to L+2.



                mov     ax, ds:[si]     ;Get data at K+2.
                add     ax, es:[di]     ;Add in data at L+2.
                mov     ds:[bx], ax     ;Store result to J+2.

                add     bx, 2           ;Move to J+4.
                add     si, 2           ;Move to K+4.
                add     di, 2           ;Move to L+4.



                mov     ax, ds:[si]     ;Get data at K+4.
                add     ax, es:[di]     ;Add in data at L+4.
                mov     ds:[bx], ax     ;Store result to J+4.

                add     bx, 2           ;Move to J+6.
                add     si, 2           ;Move to K+6.
                add     di, 2           ;Move to L+6.



                mov     ax, ds:[si]     ;Get data at K+6.
                add     ax, es:[di]     ;Add in data at L+6.
                mov     ds:[bx], ax     ;Store result to J+6.



Quit:           mov     ah, 4ch         ;Magic number for DOS
                int     21h             ; to tell this program to quit.
Main            endp

cseg            ends

sseg            segment para stack 'stack'
stk             byte    1024 dup ("stack   ")
sseg            ends

I don't understand pretty much everything in this code.I dont see how nor where do we 'Add the four words in variables J, K, and L together using pointers to these variables'

If someone could help me with this,I would higly epriciate it.
guzpapir is offline   Reply With Quote
Old Aug 8th, 2005, 4:24 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
1. ByteAry [bx] doesn not indicate that an element of ByteAry contains bx. The address of ByteAry is offset by the value in bx. Consider that you may have an integer in C/C++ serve as an index into a byte array. The index does NOT have to be the same type as the element, rather, just an offset. In addition, don't expect assembler syntax to match one for one with any other language's syntax.

2. The comments are very self-explanatory. However, there is some contradiction concerning the value of K. You don't show enough; one doesn't know if it's 4, as you state, or 123 as the comment states.

3. 0d is the ASCII code for a carriage return. 0a is the ASCII code for a line feed (newline). The 0 is a marker saying the string ends there. Sometimes a $ is used.

4. There's no point in addressing this if you don't understand indirect references (pointers). Post back with your level of expertise, including any high level languages.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 9th, 2005, 6:28 AM   #3
guzpapir
Newbie
 
Join Date: Aug 2005
Posts: 4
Rep Power: 0 guzpapir is on a distinguished road
I studied C++ earlier from one book.I am not a programer just preparing myself for the university.I think that i understand pointers in C++ and it looked like the pointers are even eaiser in Asembler.
This is what I forgot to wrote:
lea ax, J
mov Ptr1, ax ;this is needed to Initialize Ptr1 with J's address


Can u just explain me this part of code

add bx, 2 ;Move to J+2.

Before adding bx containes the address of variable J and that address is actually just the address of the first word in J.So when we add 2 does that means that we added 2 bytes and moved to the address of the seond word in variable J?
guzpapir is offline   Reply With Quote
Old Aug 9th, 2005, 7:09 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
When you add to or increment a pointer in C/C++, the compiler sees to it that the addition is in terms of the type pointed to. Assuming a 32-bit integer and byte addressing, if you add one to an integer pointer you will actually add four, to point to the next integer. Not true in assembler. These things are not "typed". BX holds an address. If you add two, it points two bytes (assuming byte addressing) farther into memory. Address 0x1000 would become 0x1002.
Address    Data
0x1000      'A'
0x1001      'B'
0x1002      'C'
0x1003      'D'
If bx were 0x1000 and you fetched the data using it indirectly you would get 'A'. If you add two and do it again, you will get 'C'.

"word" is just a word. (Never got to say that before.) Your use of it conveys nothing to your auditor unless you both know how you've defined it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC