Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

richard4339

macrumors 6502a
Original poster
Sep 6, 2006
896
112
Illinois
Albiet, this isn't specifically for a Mac, its something I'm working on for an intro to assembly class. I'm working on a program to recursively generate fibonacci numbers, and here's what I have so far...

Code:
TITLE Fibonacci
; Richard
; Feb 20, 2007

.model small
.386

.stack 4096

.DATA
	array DWORD 30 DUP(0)

INCLUDE Irvine16.inc
.code
main PROC
	mov ax,@data
	mov ds,ax
	
	mov BX, OFFSET array
	mov [bx],0
	add bx, 4
	mov [bx],1
	sub bx,4
	mov cx,0
	call fib
	mov ax, 4C00H ; setup
	int 21H ; return to DOS
ReturnMain:
	call WriteDec
	call Crlf
	exit
main ENDP

fib PROC
	mov dx,[bx]
	mov cx,[bx+4]
	add cx,dx
	mov [bx+8],cx
	add bx,4
	cmp ax,30
	jnz L1
	
L1:	inc cx
	call fib
	
fib ENDP
END main

Its giving me all kinds of errors (if I do it in protected mode, it won't let me write to [bx], now its giving me errors everywhere, including there, and multiple other lines. Anybody have any suggestions or pointers?
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
It's been a while, so please forgive me for a stupid question but isn't the .386 directive incompatible with the .model small directive? Shouldn't it be forced to large or huge memory models in 386 mode, protected or otherwise?
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
1. The assembler in XCode uses "gas" (gnu assembler) syntax, which looks quite a bit different from what you have.

2. Int 21 doesn't have one bit of a chance to run on a Macintosh. There is no DOS layer carefully hidden below MacOS X.
 

richard4339

macrumors 6502a
Original poster
Sep 6, 2006
896
112
Illinois
Our teacher is a Mac nut like myself, but the lab is a Windows lab, so we're using MASM for Windows to turn in for the assignments, which I've been unsuccessful in getting working on my MacBook Pro yet in Parellels or Bootcamp.

As far as the .386 and the .model small, he has specifically told us to use those two directives, along with the int 21 to exit the program. I'm not actually sure how the int 21 will work on our machines at school either, though, since we definately do not have DOS on our machines...
 

cppnerd

macrumors newbie
Jan 19, 2007
25
0
As far as the .386 and the .model small, he has specifically told us to use those two directives, along with the int 21 to exit the program. I'm not actually sure how the int 21 will work on our machines at school either, though, since we definately do not have DOS on our machines...

Are you building in the Mac or Windows environment? If you are doing assembly in Mac OS, for x86, you have natively incompatible code for sure because of the int 21.

As far as bootcamp goes, I would not be suprised if Apple does not support the hidden DOS layer in windows that allows legacy code like that to be executed. Remember that Apple does not support the Windows 9x kernel through bootcamp; I suspect any legacy calls from XP are simply caught in a handler and the behavior is mimicked as need be.

Try building it on a regular PC; if you still get errors let us know.
Good luck.
 

richard4339

macrumors 6502a
Original poster
Sep 6, 2006
896
112
Illinois
Definite errors in boot camp; errors in Windows on lab machines as well. The errors I mentioned above actually were the ones from my lab machine.
 

MarkCollette

macrumors 68000
Mar 6, 2003
1,559
36
Toronto, Canada
Step 1: Write and debug algorithm in C
Step 2: Have the C compiler generate the assembly for you
Step 3: Use that as a template for any assembler work you'll be doing
 

SC68Cal

macrumors 68000
Feb 23, 2006
1,642
0
As far as bootcamp goes, I would not be suprised if Apple does not support the hidden DOS layer in windows that allows legacy code like that to be executed. Remember that Apple does not support the Windows 9x kernel through bootcamp; I suspect any legacy calls from XP are simply caught in a handler and the behavior is mimicked as need be.

Forgive me if I'm wrong, but I was under the impression that Boot Camp was just a drivers and EFI configuration, to allow you to run windows.

It's the same hardware as any other PC laptop.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
Forgive me if I'm wrong, but I was under the impression that Boot Camp was just a drivers and EFI configuration, to allow you to run windows.

It's the same hardware as any other PC laptop.

That's my impression, also, and that it emulates BIOS.

Any DOS layer would be a part of Windows or not. The Windows NT (WinNT, Win2000, WinXP, Vista) base doesn't handle the DOS calls quite the same way that the graphical shells like Win98 do.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
That's my impression, also, and that it emulates BIOS.

Not quite, the firmware updates emulate the BIOS. Bootcamp (the app) is just a shiny CD burning utility and disk partitioner. No need to install it at all to get XP installed.

Any DOS layer would be a part of Windows or not. The Windows NT (WinNT, Win2000, WinXP, Vista) base doesn't handle the DOS calls quite the same way that the graphical shells like Win98 do.

This would be correct. Interrupts are setup by the OS, using BIOS. So when you use int 21, that call is rerouted using the BIOS interrupt table back into the OS. It used to be the quick way to call into the kernel, and int 21 worked on NT-based systems last time I wrote some assembler. It is the responsibility of the OS to setup the interrupt table.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Code:
TITLE Fibonacci
; Richard
; Feb 20, 2007

.model small
.386

.stack 4096

.DATA
	array DWORD 30 DUP(0) 

INCLUDE Irvine16.inc
.code
start:     [COLOR="blue"]; Main is not a procedure in assembler[/COLOR]
	mov ax,@data
	mov ds,ax
	
	mov BX, OFFSET array
	mov [bx],0
	add bx, 4
	mov [bx],1
	sub bx,4
	mov cx,0
	call fib
	mov ax, 4C00H ; setup
	int 21H ; return to DOS
ReturnMain: [COLOR="Blue"]; What is this doing? Nothing references it[/COLOR]
	call WriteDec 
	call Crlf 
	exit

fib PROC
	mov dx,[bx]
	mov cx,[bx+4]
	add cx,dx
	mov [bx+8],cx
	add bx,4
	cmp ax,30
	jnz L1
	[COLOR="blue"]; Something is missing in this line. You jnz to L1, but if the result /is/ 0, then you are just effectively jumping to L1 anyways. Should a ret go here too?[/COLOR]
L1:	inc cx
	call fib
	ret [COLOR="Blue"]; You really should (for safety) terminate your functions with an actual return call.[/COLOR]
fib ENDP
END start [COLOR="blue"]; Your code segment block, and your main procedure should not be one and the same.[/COLOR]

Its giving me all kinds of errors (if I do it in protected mode, it won't let me write to [bx], now its giving me errors everywhere, including there, and multiple other lines. Anybody have any suggestions or pointers?

My comments and edits are inline. :)

Now, my assembler is rusty, but this should at least point you in the right direction on a couple areas.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.