flat assembler
Message board for the users of flat assembler.

Index > DOS > Newbie : DOS program from a book wont run

Goto page 1, 2, 3, 4, 5, 6  Next
Author
Thread Post new topic Reply to topic
radarblue



Joined: 10 Oct 2016
Posts: 44
Location: Norway, Oslo
radarblue 10 Oct 2016, 20:25
Good day.
I am reading a book called : x86 assembly language and C fundamentals, by J. Cavanagh. I have read the usual basics, and made some small C programs that runs. Now I am on page 200 in the book, and examples proceedes in assembly. And again, equally lost as I were when getting a C program to run, the asm wont run. The book suggests flat assembler. I get errors on every line. Error on capital letters, and error on the "proc far".

I tried to make the capital letters into small letters
and add this include 'win64ax.inc', but no function.

This is the code, in exact type from the book.

Code:
;swap_bytes.asm
;--------------
.STACK
;--------------
.DATA
TEMP DB ? ;DB=define bytes
;$ delimiter meaning end of string
RSLT DB 0DH, 0AH, 'BL = , BH = $'
;--------------
.CODE
BEGIN PROC FAR
;set up pgm ds
MOV AX,@DATA ; place .DATA addr in ax
MOV DS,AX

;assign values to bl and bh
MOV BL,'A'
MOV BH,'B'

;store bl in temp area before swapping
MOV TEMP, BL

;swap registers
MOV BL, BH
MOV BH,TEMP

;move registers to result area for display
MOV RSLT + 7,BL
MOV RSLT + 15,BH

;print result
MOV AH,09H              ;display string
MOV DX, OFFSET RSLT ;rslt address must be in dx
INT 21H                    ;a dos interrupter that uses fctn (?) code in ah

BEGIN ENDP               ;starts pgm at begin (!)
END    
Post 10 Oct 2016, 20:25
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 10 Oct 2016, 20:51
corrected code:
Code:
        format  MZ
        entry   MainSeg:BEGIN
        stack   1024

segment MainSeg

BEGIN:
;set up pgm ds
MOV AX,DataSeg ; place .DATA addr in ax
MOV DS,AX

;assign values to bl and bh
MOV BL,'A'
MOV BH,'B'

;swap registers
XCHG BL,BH

;move registers to result area for display
MOV [TheBL],BL
MOV [TheBH],BH

;print result
MOV AH,09H              ;display string
MOV DX,RSLT             ;rslt address must be in dx
INT 21H                 ;a dos interrupter that uses fctn (?) code in ah

MOV AH,4Ch
INT 21h

segment DataSeg

;$ delimiter meaning end of string
RSLT DB 0DH, 0AH, ' BL = '
TheBL DB '?',', BH = '
TheBH DB '?','$'
;--------------
    

_________________
smaller is better
Post 10 Oct 2016, 20:51
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 11 Oct 2016, 15:11
radarblue: The code you posted appears to be in MASM syntax. And it also appears to be very old DOS code. 64-bit versions of Windows won't be able to run the DOS code unless you install something like DOSBOX or a similar program.
Post 11 Oct 2016, 15:11
View user's profile Send private message Visit poster's website Reply with quote
radarblue



Joined: 10 Oct 2016
Posts: 44
Location: Norway, Oslo
radarblue 11 Oct 2016, 15:28
Thanks alot Mr. Candyman, and Mr. Revolution

Still doesnt run, in the FASM editor.

Error message : "....\swap_bytes.exe " cannot run due to incompatability with 64-bit versions of windows .

When I write the : include 'win64ax.inc' on the top, or include 'win32ax.inc' .
Error message : format MZ ( the first line )

There is a compatability failure, it seems .
Mr. Revolution, I will try your advice and install the DOSbox and MASM.
Post 11 Oct 2016, 15:28
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 11 Oct 2016, 18:46
radarblue wrote:
Error message : "....\swap_bytes.exe " cannot run due to incompatability with 64-bit versions of windows .
When the CPU is in 64-bit mode there is no way to enable 16-bit v8086 mode, so instead you need an emulator (i.e. DOSBOX or equivalent).
radarblue wrote:
Mr. Revolution, I will try your advice and install the DOSbox and MASM.
I didn't suggest installing MASM, but you can do that if you want to save you having to convert the book examples into fasm syntax,

BTW, your assumption about "Mr. Revolution" might not be correct. Half of the people on this planet are not Mr.
Post 11 Oct 2016, 18:46
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4330
Location: Now
edfed 11 Oct 2016, 21:56
lol
Post 11 Oct 2016, 21:56
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 11 Oct 2016, 23:53
radarblue wrote:
I am reading a book called : x86 assembly language and C fundamentals, by J. Cavanagh. I have read the usual basics, and made some small C programs that runs. Now I am on page 200 in the book, and examples proceedes in assembly. And again, equally lost as I were when getting a C program to run, the asm wont run. The book suggests flat assembler. I get errors on every line. Error on capital letters, and error on the "proc far".

I tried to make the capital letters into small letters
and add this include 'win64ax.inc', but no function.


I'm pretty sure that the book's author is not telling you to use FASM here. MASM is a radically different language. Especially regarding 16-bit DOS (which even MASM has shunned for decades), FASM won't work for linking since it doesn't support OMF output.

If you really want to learn MASM, try these links:



All of the high-level stuff is different. The only similarity is raw x86 mnemonics. FASM does not try to support MASM (or TASM or ...) syntax at all (unlike JWasm, which is forked/heavily improved from OpenWatcom's WASM). Most MASM code (for many years) has been Win32/64 only, but it was still popular in DOS days too, so you'll still find old code.

I'm also not sure if you're trying to mix C and assembly or not. FASM doesn't really support that for 16-bit DOS (but 32-bit COFF for DJGPP is okay).

If you want to mix 16-bit assembly and C, one example is the Calvin editor (Turbo C + A86): calvin23.zip, calsrc23.zip .

If you just want to learn 32-bit C and assembly, try reading Dr. Paul Carter's free book (using NASM): http://pacman128.github.io/pcasm/

P.S. I'd honestly recommend VirtualBox (VT-X) + FreeDOS over (slow, limited) DOSBox.
Post 11 Oct 2016, 23:53
View user's profile Send private message Visit poster's website Reply with quote
radarblue



Joined: 10 Oct 2016
Posts: 44
Location: Norway, Oslo
radarblue 12 Oct 2016, 20:44
Thanks alot. Its boiling over ...
However I want to spend time on it and understand.

rugxolo :
rugxulo wrote:
I'm also not sure if you're trying to mix C and assembly or not. FASM doesn't really support that for 16-bit DOS (but 32-bit COFF for DJGPP is okay).
The book x86 and C fundamentals is a book I like very much, and it concerns inline C to ASM. However I need to get the program to run the assembly code to start experimenting, and get the theory into practice. I need to find a platform (editor) where the code runs, looking more or less like that in the book to follow up the examples .

In the book (p209) the Author lists up many assemblers, MASM, NASM, LZASM, NBASM, FASM, codeX assembler, TMA macro assembler, and continues this book uses a "version" of flat assembler. Im not a typical "computer guy", but have caught up an old and deep facination in the recent years.
Thank you for the links and support, I will investigate .

Checked out : emu8086, and it seems very interesting. And also checked DOSbox with MASM. however I could only load the qeditor in a window, and could not access any "edit" or "debug" from the CMD prom. Althou the code seems decently alike that style, noticably the @data.

Question. how can it be that many different assemblers run on the same chipset architecture x86, and some codes just doesnt, like the code I have in the book ?

revolution :
I am sorry, I was trying to be polite.
Post 12 Oct 2016, 20:44
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12739
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 12 Oct 2016, 22:02
here page 209, Very Happy

Image
Post 12 Oct 2016, 22:02
View user's profile Send private message Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 257
Trinitek 13 Oct 2016, 04:43
Quote:
Question. how can it be that many different assemblers run on the same chipset architecture x86, and some codes just doesnt, like the code I have in the book ?
x86 has different processor modes that change which registers, instructions, and amount of memory is available.

1) certain 16-bit opcodes aren't encodable in 64-bit protected mode ("Long mode")

2) Virtual-8086 mode, which Windows uses to run 16-bit executables, isn't accessible when in 64-bit mode.

And thus, only 32-bit Windows versions can run 16-bit code. Although, while it isn't technically impossible to step down into a processor mode that can run 16-bit code, the transition would have been too expensive and too much of a hassle to implement.
Post 13 Oct 2016, 04:43
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 13 Oct 2016, 13:47
Trinitek wrote:
Although, while it isn't technically impossible to step down into a processor mode that can run 16-bit code, the transition would have been too expensive and too much of a hassle to implement.
There would be a lot of security problems also because it seems that MS have a lot of trouble to make these things work without silly side effects. Plus all the effort required would be time not well spent to support the remaining three people left in the world that still need native DOS code to run.
Post 13 Oct 2016, 13:47
View user's profile Send private message Visit poster's website Reply with quote
radarblue



Joined: 10 Oct 2016
Posts: 44
Location: Norway, Oslo
radarblue 13 Oct 2016, 15:34
ok so it would be safe to say that there is an unique assembler "language" for each processor, and for each bit level, like x86, 16bit, 32bit and 64 bit. and there would also be an individual assembly language for the different chips . Like Amiga motorola 68060, will have its own ASM structure. along with an IBM processor, Intel Processor or AMD. Or flashing or progamming a washing machine logic, traffic lights or a motor controller .

It seems clear that I am onto the Intel x86 16/32bit for starters. I would also presume all the different assemblers have something in common, since the basic theory seems to be more or less uniform. But the Assemblers headers and program structure is different from processor to processor, and bit level to bit level.


Last edited by radarblue on 13 Oct 2016, 15:48; edited 2 times in total
Post 13 Oct 2016, 15:34
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 13 Oct 2016, 15:46
radarblue wrote:
ok so it would be safe to say that there is an unique assembler "language" for each processor, and for each bit level, like x86, 16bit, 32bit and 64 bit. and there would also be an individual assembly language for the different chips .
Yes, that is correct. But it goes deeper than that. Even among all 32-bit CPUs there are differences in supported instructions. For example instructions that use MMX, SSE etc. are not universally supported in all CPUs. And the same for 64-bit CPUs with newer things like AVX.
Post 13 Oct 2016, 15:46
View user's profile Send private message Visit poster's website Reply with quote
radarblue



Joined: 10 Oct 2016
Posts: 44
Location: Norway, Oslo
radarblue 13 Oct 2016, 16:04
The first thing I should do is to obtain a CPU simulator or a real CPU (like the one I have in my own machine) . Then study that CPUs specific instruction set, then apply the assembly programming language via a proper editor.

1. find the target CPU to program .
2. obtain the CPU`s instruction set .
3. apply assembly code to the CPU instruction set, from an editor .

I am going to try out the EMU8086 , the .EXE header gives a nice starting point.

The emu8086 is related to fasm, so I presum its relevant for the forum, however maybe not in the DOS thread ? Anybody that is familiar with the emu8086 ?
Post 13 Oct 2016, 16:04
View user's profile Send private message Visit poster's website Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 257
Trinitek 13 Oct 2016, 19:57
radarblue wrote:
The emu8086 is related to fasm, so I presum its relevant for the forum, however maybe not in the DOS thread ? Anybody that is familiar with the emu8086 ?
I used emu8086 when I first started. IIRC it uses an old version of FASM as a backend assembler, but the manual and tutorials will teach you macros and directives that are emu8086-specific. Even so, we shouldn't have any problems helping you if you need assistance.
Post 13 Oct 2016, 19:57
View user's profile Send private message Reply with quote
radarblue



Joined: 10 Oct 2016
Posts: 44
Location: Norway, Oslo
radarblue 13 Oct 2016, 20:23
I am very proud ! I managed to get a print !
Using the EMU8086 editor, on a 64bit CPU, no win32bit emulator.
Code:
     .stack
     .data
temp db ?
rslt db 0dh, 0ah, "bl =  , bh =  $" 
;dh = cursor position
;ah = linefeed
;$ = string terminator

     .code
proc
mov ax,@data
mov ds,ax

mov bl,'A'
mov bh,'B'

mov temp,bl

mov bl,bh
mov bh,temp

mov rslt + 7,bl
mov rslt + 15,bh

mov ah,09h
mov dx, offset rslt

int 21h
endp    


the error was that the book said :
rslt db 0dh, 0ah, 'bl = , bh = $'

while the editor tolerated this
rslt db 0dh, 0ah, "bl = , bh = $"

rugxolo :
the link you provided from " the Art of Assembly". Is a mother load of valuable information. C, assembly, logic circuits and BCD.
http://www.plantation-productions.com/Webster/www.artofasm.com/DOS/pdfaoa.zip

sleepsleep :
Thats the book ! Very Happy


Last edited by radarblue on 14 Oct 2016, 14:34; edited 3 times in total
Post 13 Oct 2016, 20:23
View user's profile Send private message Visit poster's website Reply with quote
MrFox



Joined: 17 Aug 2016
Posts: 52
Location: Russia
MrFox 14 Oct 2016, 06:12
Yay! Congrats!
Can you tell us what made you learn specifically DOS-assembler? DOS is a Dead OS, you're never going to run into it on new computers. Windows/Linux/Unix/UEFI or bare metal. No other options, I guess.
So, why?
Post 14 Oct 2016, 06:12
View user's profile Send private message Reply with quote
radarblue



Joined: 10 Oct 2016
Posts: 44
Location: Norway, Oslo
radarblue 14 Oct 2016, 13:52
Id be happy to. I have always been facinated by computers, but never "engaged", I am typically a "user", audio and graphix. But took an education into classical art. Had to get re-schooled, pay my bills. and went for Electro. Got a job in the high voltage domain.

In the Electro schoolbooks, particularely Automation. We had something called "Ladder programming" PLC and relay coil control, for motors on and off, factory conveyor belts. alert sounds and lights. And I figured it was a great way to finally start to understand the computer. I have always also loved the Amiga. and assembly its something mystical and inspiring about it. Bought the book X86 and C fundamentals. And here I am ! As far as I understand the emu8086 is a 32bit and not a DOS assembler. However, I like to learn from scratch. From the circuits to software. For example Embedded Systems.
Post 14 Oct 2016, 13:52
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 14 Oct 2016, 14:09
I'd suggest you abandon DOS (and the ugly segment registers) and try some 32-bit code. It will be both more relevant to current computers as they are used today and it doesn't complicate things with a limited selection of address registers or the needless segment register complexities. Plus no need for a VM or emulator, it can run natively.
Post 14 Oct 2016, 14:09
View user's profile Send private message Visit poster's website Reply with quote
radarblue



Joined: 10 Oct 2016
Posts: 44
Location: Norway, Oslo
radarblue 14 Oct 2016, 14:47
I see you point revolution. But since I am following this certain book, and it focus on Inline C and asm code. The inlines allow for this particular (DOS...) assembly code. And since I am not familiar with the syntax this is my starting point. All in good time.
Post 14 Oct 2016, 14:47
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3, 4, 5, 6  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.