PDA

View Full Version : Assembly x86 question




richard4339
Feb 20, 2007, 12:59 AM
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...


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
Feb 20, 2007, 02:28 AM
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
Feb 20, 2007, 02:31 AM
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
Feb 20, 2007, 02:15 PM
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
Feb 20, 2007, 02:39 PM
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
Feb 20, 2007, 04:50 PM
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
Feb 20, 2007, 05:49 PM
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
Feb 27, 2007, 12:42 AM
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
Feb 27, 2007, 01:51 AM
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
Feb 27, 2007, 10:39 AM
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
Feb 27, 2007, 10:59 AM
TITLE Fibonacci
; Richard
; Feb 20, 2007

.model small
.386

.stack 4096

.DATA
array DWORD 30 DUP(0)

INCLUDE Irvine16.inc
.code
start: ; Main is not a procedure in assembler
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: ; What is this doing? Nothing references it
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
; 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?
L1: inc cx
call fib
ret ; You really should (for safety) terminate your functions with an actual return call.
fib ENDP
END start ; Your code segment block, and your main procedure should not be one and the same.


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.

GeeYouEye
Feb 27, 2007, 11:05 AM
FFR, can someone share a link to an x86 assembly reference? I've never done any x86 work, so I don't know what's good and bad out there.