flat assembler
Message board for the users of flat assembler. Index > Main > [SOLVED] converting MASM to FASM (3d array).
Author
 Thread  FASMNOOB

Joined: 09 Feb 2012
Posts: 21
FASMNOOB
So far I have the following code with an error on line 16. I gutted the original 16 bit MASM code below it. Is there something I'm missing with the data segment part where I declare the variables?

Code:
```; Multidimensional Array declaration and access

org 100h; code starts at offset 100h
use16; use 16-bit code

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

; AL := B2Ary2[j,k]

mov     bx, J           ;index := (j*4+k)
mov     al, B2Ary2[bx]

; AX := W2Ary2[j,k]

mov     ax, J           ;index := (j*3 + k)*2
mov     bx, 3
mul     bx              ;(j*3)-- This destroys DX!
mov     bx, ax
mov     ax, W2Ary2[bx]

; EAX := D2Ary[i,j]

mov     ax, J           ;index := (j*6 + k)*4
mov     bx, 6
mul     bx              ;DX:AX := j*6, ignore overflow in DX.
add     ax, k           ;j*6 + k
add     ax, ax          ;(j*6 + k)*2
add     ax, ax          ;(j*6 + k)*4
mov     bx, ax
mov     eax, D2Ary[bx]

; Sample access of a three dimensional array.
;
; AX := W3Ary[J,K,L]

mov     ax, J           ;index := ((j*3 + k)*4 + l)*2
mov     bx, 3
mul     bx              ;j*3
add     ax, K           ;j*3 + k
add     ax, ax          ;(j*3 + k)*2
add     ax, ax          ;(j*3 + k)*4
add     ax, l           ;(j*3 + k)*4 + l
add     ax, ax          ;((j*3 + k)*4 + l)*2
mov     bx, ax
mov     ax, W3Ary[bx]

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

J               word    1
K               word    2
L               word    3

; Some two-dimensional arrays.
; Note how this code uses the "dup" operator to suggest the size
; of each dimension.

B2Ary           byte    3 dup (4 dup (?))
W2Ary           word    4 dup (3 dup (?))
D2Ary           dword   2 dup (6 dup (?))

; 2D arrays with initialization.
; Note the use of data layout to suggest the sizes of each array.

B2Ary2          byte    0, 1, 2, 3
byte    4, 5, 6, 7
byte    8, 9, 10, 11

W2Ary2          word    0,  1,  2
word    3,  4,  5
word    6,  7,  8
word    9, 10, 11

D2Ary2          dword   0,  1,  2,  3,  4,  5
dword   6,  7,  8,  9, 10, 11

; A sample three dimensional array.

W3Ary           word    2 dup (3 dup (4 dup (?)))
```

Code:
```; Multidimensional Array declaration and access
;
; Randall Hyde

.386                            ;Need these two statements to
option  segment:use16               ; use the 80386 register set.

dseg           segment para public 'data'

; Indices we will use for the arrays.

J          word    1
K          word    2
L          word    3

; Some two-dimensional arrays.
; Note how this code uses the "dup" operator to suggest the size
; of each dimension.

B2Ary              byte    3 dup (4 dup (?))
W2Ary              word    4 dup (3 dup (?))
D2Ary              dword   2 dup (6 dup (?))

; 2D arrays with initialization.
; Note the use of data layout to suggest the sizes of each array.

B2Ary2            byte    0, 1, 2, 3
byte    4, 5, 6, 7
byte    8, 9, 10, 11

W2Ary2              word    0,  1,  2
word    3,  4,  5
word    6,  7,  8
word    9, 10, 11

D2Ary2         dword   0,  1,  2,  3,  4,  5
dword   6,  7,  8,  9, 10, 11

; A sample three dimensional array.

W3Ary           word    2 dup (3 dup (4 dup (?)))

dseg           ends

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.

; AL := B2Ary2[j,k]

mov     bx, J           ;index := (j*4+k)
mov     al, B2Ary2[bx]

; AX := W2Ary2[j,k]

mov     ax, J           ;index := (j*3 + k)*2
mov     bx, 3
mul     bx              ;(j*3)-- This destroys DX!
mov     bx, ax
mov     ax, W2Ary2[bx]

; EAX := D2Ary[i,j]

mov     ax, J           ;index := (j*6 + k)*4
mov     bx, 6
mul     bx              ;DX:AX := j*6, ignore overflow in DX.
add     ax, k           ;j*6 + k
add     ax, ax          ;(j*6 + k)*2
add     ax, ax          ;(j*6 + k)*4
mov     bx, ax
mov     eax, D2Ary[bx]

; Sample access of a three dimensional array.
;
; AX := W3Ary[J,K,L]

mov     ax, J           ;index := ((j*3 + k)*4 + l)*2
mov     bx, 3
mul     bx              ;j*3
add     ax, K           ;j*3 + k
add     ax, ax          ;(j*3 + k)*2
add     ax, ax          ;(j*3 + k)*4
add     ax, l           ;(j*3 + k)*4 + l
add     ax, ax          ;((j*3 + k)*4 + l)*2
mov     bx, ax
mov     ax, W3Ary[bx]

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

zzzzzzseg   segment para public 'zzzzzz'
LastBytes     byte    16 dup (?)
zzzzzzseg ends
end     Main

```

Last edited by FASMNOOB on 10 Feb 2012, 17:26; edited 1 time in total 10 Feb 2012, 05:08  revolution When all else fails, read the source Joined: 24 Aug 2004 Posts: 17467 Location: In your JS exploiting you and your system revolution Hints: Replace "dseg segment para public 'data'" with "segment dseg" Remove "dseg ends" Replace "byte" with "db" Replace "word" with "dw" Replace "dword" with "dd" 10 Feb 2012, 14:00
FASMNOOB

Joined: 09 Feb 2012
Posts: 21
FASMNOOB
What about cseg, the part "assume cs:cseg, ds:dseg"? anyways now I get an error around the part with the dseg. Do I always need to use org 100h?

Code:
```
org 100h; code starts at offset 100h
use16; use 16-bit code

segment dseg

; Indices we will use for the arrays.

J               dw    1
K               dw    2
L               dw    3

; Some two-dimensional arrays.
; Note how this code uses the "dup" operator to suggest the size
; of each dimension.

B2Ary           db    3 dup (4 dup (?))
W2Ary           dw    4 dup (3 dup (?))
D2Ary           dd   2 dup (6 dup (?))

; 2D arrays with initialization.
; Note the use of data layout to suggest the sizes of each array.

B2Ary2          db    0, 1, 2, 3
db    4, 5, 6, 7
db    8, 9, 10, 11

W2Ary2          dw    0,  1,  2
dw    3,  4,  5
dw    6,  7,  8
dw    9, 10, 11

D2Ary2          dd   0,  1,  2,  3,  4,  5
dd   6,  7,  8,  9, 10, 11

; A sample three dimensional array.

W3Ary           dw    2 dup (3 dup (4 dup (?)))

;dseg            ends

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.

; AL := B2Ary2[j,k]

mov     bx, J           ;index := (j*4+k)
mov     al, B2Ary2[bx]

; AX := W2Ary2[j,k]

mov     ax, J           ;index := (j*3 + k)*2
mov     bx, 3
mul     bx              ;(j*3)-- This destroys DX!
mov     bx, ax
mov     ax, W2Ary2[bx]

; EAX := D2Ary[i,j]

mov     ax, J           ;index := (j*6 + k)*4
mov     bx, 6
mul     bx              ;DX:AX := j*6, ignore overflow in DX.
add     ax, k           ;j*6 + k
add     ax, ax          ;(j*6 + k)*2
add     ax, ax          ;(j*6 + k)*4
mov     bx, ax
mov     eax, D2Ary[bx]

; Sample access of a three dimensional array.
;
; AX := W3Ary[J,K,L]

mov     ax, J           ;index := ((j*3 + k)*4 + l)*2
mov     bx, 3
mul     bx              ;j*3
add     ax, K           ;j*3 + k
add     ax, ax          ;(j*3 + k)*2
add     ax, ax          ;(j*3 + k)*4
add     ax, l           ;(j*3 + k)*4 + l
add     ax, ax          ;((j*3 + k)*4 + l)*2
mov     bx, ax
mov     ax, W3Ary[bx]

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             db    1024 dup ("stack   ")
sseg            ends

zzzzzzseg       segment para public 'zzzzzz'
Lastdbs       db    16 dup (?)
zzzzzzseg       ends
end     Main
``` 10 Feb 2012, 15:21  revolution When all else fails, read the source Joined: 24 Aug 2004 Posts: 17467 Location: In your JS exploiting you and your system revolution Well the dseg replacement above was an example intended to be applied to all the segment statements. Remove all the "ends" lines. Just delete the "assume" thing. And also replace "proc" with a colon (:) and delete the endp line since you don't have any macros to define what "proc" means. If you use org 0x100 then you probably want to create a com file, but the code suggests you want an exe file. Try using format mz. See the example in the DOS fasm download for "multiseg". Last edited by revolution on 10 Feb 2012, 16:57; edited 1 time in total 10 Feb 2012, 15:51
FASMNOOB

Joined: 09 Feb 2012
Posts: 21
FASMNOOB
Thanks so far, I made the following changes below. Now its the declaration in the data segment thats wrong, not sure why. I did follow this page to get an idea for the MZ format. Is that also 16 bit mode or is it also 32 bit as well?

This is the error
B2Ary db 3 dup (4 dup (?))

Code:
```format MZ
;org 100h; code starts at offset 100h
use16; use 16-bit code
entry cseg:Main
stack 100h

segment dseg
; Indices we will use for the arrays.

J               dw    1
K               dw    2
L               dw    3

; Some two-dimensional arrays.
; Note how this code uses the "dup" operator to suggest the size
; of each dimension.

B2Ary           db    3 dup (4 dup (?))
W2Ary           dw    4 dup (3 dup (?))
D2Ary           dd   2 dup (6 dup (?))

; 2D arrays with initialization.
; Note the use of data layout to suggest the sizes of each array.

B2Ary2          db    0, 1, 2, 3
db    4, 5, 6, 7
db    8, 9, 10, 11

W2Ary2          dw    0,  1,  2
dw    3,  4,  5
dw    6,  7,  8
dw    9, 10, 11

D2Ary2          dd   0,  1,  2,  3,  4,  5
dd   6,  7,  8,  9, 10, 11

; A sample three dimensional array.

W3Ary           dw    2 dup (3 dup (4 dup (?)))

;dseg            ends

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

; AL := B2Ary2[j,k]

mov     bx, J           ;index := (j*4+k)
mov     al, B2Ary2[bx]

; AX := W2Ary2[j,k]

mov     ax, J           ;index := (j*3 + k)*2
mov     bx, 3
mul     bx              ;(j*3)-- This destroys DX!
mov     bx, ax
mov     ax, W2Ary2[bx]

; EAX := D2Ary[i,j]

mov     ax, J           ;index := (j*6 + k)*4
mov     bx, 6
mul     bx              ;DX:AX := j*6, ignore overflow in DX.
add     ax, k           ;j*6 + k
add     ax, ax          ;(j*6 + k)*2
add     ax, ax          ;(j*6 + k)*4
mov     bx, ax
mov     eax, D2Ary[bx]

; Sample access of a three dimensional array.
;
; AX := W3Ary[J,K,L]

mov     ax, J           ;index := ((j*3 + k)*4 + l)*2
mov     bx, 3
mul     bx              ;j*3
add     ax, K           ;j*3 + k
add     ax, ax          ;(j*3 + k)*2
add     ax, ax          ;(j*3 + k)*4
add     ax, l           ;(j*3 + k)*4 + l
add     ax, ax          ;((j*3 + k)*4 + l)*2
mov     bx, ax
mov     ax, W3Ary[bx]

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

segment       sseg
stk             db    1024 dup ("stack   ")

segment zzzzzzseg
Lastdbs       db    16 dup (?)
``` 10 Feb 2012, 16:19  revolution When all else fails, read the source Joined: 24 Aug 2004 Posts: 17467 Location: In your JS exploiting you and your system revolution You are almost there. fasm doesn't support nested dup operations, you will need to flatten them to one level: db (3*4) dup ? Also this: B2Ary2[bx] will need to be changed to this: [B2Ary2+bx] 10 Feb 2012, 17:04
FASMNOOB

Joined: 09 Feb 2012
Posts: 21
FASMNOOB
Thanks, looks like the following compiles. I just have two more questions.

1. Why does he use this " mov es, ax ; segment register."?
2. How would I layout a 3d array. notice he only gave examples for the
2d case.

Code:
```format MZ
;org 100h; code starts at offset 100h
;use16; use 16-bit code
entry cseg:Main
;stack 100h

;dseg            ends

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

; AL := B2Ary2[j,k]

mov     bx, J           ;index := (j*4+k)
mov     al, [B2Ary2+bx]

; AX := W2Ary2[j,k]

mov     ax, J           ;index := (j*3 + k)*2
mov     bx, 3
mul     bx              ;(j*3)-- This destroys DX!
mov     bx, ax
mov     ax, [W2Ary2+bx]

; EAX := D2Ary[i,j]

mov     ax, J           ;index := (j*6 + k)*4
mov     bx, 6
mul     bx              ;DX:AX := j*6, ignore overflow in DX.
add     ax, K           ;j*6 + k
add     ax, ax          ;(j*6 + k)*2
add     ax, ax          ;(j*6 + k)*4
mov     bx, ax
mov     eax, [D2Ary+bx]

; Sample access of a three dimensional array.
;
; AX := W3Ary[J,K,L]

mov     ax, J           ;index := ((j*3 + k)*4 + l)*2
mov     bx, 3
mul     bx              ;j*3
add     ax, K           ;j*3 + k
add     ax, ax          ;(j*3 + k)*2
add     ax, ax          ;(j*3 + k)*4
add     ax, L           ;(j*3 + k)*4 + l
add     ax, ax          ;((j*3 + k)*4 + l)*2
mov     bx, ax
mov     ax, [W3Ary+bx]

Quit:           mov     ah, 4ch         ;Magic number for DOS
int     21h             ; to tell this program to quit.
segment dseg
; Indices we will use for the arrays.

J               dw    1
K               dw    2
L               dw    3

; Some two-dimensional arrays.
; Note how this code uses the "dup" operator to suggest the size
; of each dimension.

B2Ary           db    (3*4) dup ? ;3 dup (4 dup (?))
W2Ary           dw    (4*3) dup ? ; 4 dup (3 dup (?))
D2Ary           dd    (2*6) dup ? ;2 dup (6 dup (?))

; 2D arrays with initialization.
; Note the use of data layout to suggest the sizes of each array.

B2Ary2          db    0, 1, 2, 3
db    4, 5, 6, 7
db    8, 9, 10, 11

W2Ary2          dw    0,  1,  2
dw    3,  4,  5
dw    6,  7,  8
dw    9, 10, 11

D2Ary2          dd   0,  1,  2,  3,  4,  5
dd   6,  7,  8,  9, 10, 11

; A sample three dimensional array.

W3Ary           dw   (2*3*4) dup ? ;2 dup (3 dup (4 dup (?)))

;segment       sseg
;stk             db    1024 dup ("stack   ")

;segment zzzzzzseg
;Lastdbs       db    16 dup (?)

``` 10 Feb 2012, 17:14  revolution When all else fails, read the source Joined: 24 Aug 2004 Posts: 17467 Location: In your JS exploiting you and your system revolution FASMNOOB wrote:1. Why does he use this " mov es, ax ; segment register."? Don't know. Maybe just in case you want to use movs[b|w|d] in the future? FASMNOOB wrote:2. How would I layout a 3d array. notice he only gave examples for the 2d case. Maybe like this? Code:```W3Ary dw (2*3*4) dup ? ;2 dup (3 dup (4 dup (?))) W3Ary2 db 0, 1, 2, 3 db 4, 5, 6, 7 db 8, 9, 10, 11 ; db 20, 21, 22, 23 db 24, 25, 26, 27 db 28, 29, 30, 31 ``` 10 Feb 2012, 17:21
FASMNOOB

Joined: 09 Feb 2012
Posts: 21
FASMNOOB
thanks, that solves it. 10 Feb 2012, 17:25  Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First  Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsCompiler InternalsIDE DevelopmentOS ConstructionNon-x86 architecturesHigh Level LanguagesProgramming Language DesignProjects and IdeasExamples and Tutorials Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum