Game-Programming-Gurus-Reloaded icon indicating copy to clipboard operation
Game-Programming-Gurus-Reloaded copied to clipboard

Chapter 2 Assembler Questions

Open BGMP opened this issue 1 year ago • 4 comments

Hi,

Thank you for collecting all of these files and keeping them up on this repo. I knew I'd have to look them up eventually, and here they are! All together in one single place 😄 .

About my question now... well, I've been working through some of the Assembler listings in chapter 2 again, and I can't seem to get any of them to assemble at all.

I got MASM 6.1 running and all, but either I'm missing the point of these listings entirely or this is not the version of MASM I'm supposed to be using to assemble these programs.

For instance, "Listing 2.3. A modified version of Add_Int" should, I believe, be a straight-forward assembler program, which again, should assemble correctly when run with MASM 6.1. Here's the listing taken directly from the book:

.MODEL MEDIUM, C

.CODE

PUBLIC _Add_Int

_Add_Int PROC USES integer_1:WORD, integer_2:WORD

mov AX,integer_1
add AX,integer_2

_Add_Int ENDP

END

Assembling and linking the aforementioned program results in the following errors, according to MASM:

> ml /c /Cx /W2 /WX /nologo /Fo.\SAMPLE.obj SAMPLE.ASM
Assembling: SAMPLE.ASM
SAMPLE.ASM(7): error A2008: syntax error : integer_1
SAMPLE.ASM(12): fatal error A1010: unmatched block nesting : Add_Int
NMAKE : fatal error u1077: 'ML' : return code '1'
Stop.

I'm thinking maybe these aren't to be taken literally and that I'm just missing some of the segments like .DATA, which you normally see in ASM examples, but nowhere in the paragraphs around these listings is it hinted that these are only for illustration purposes; they rather give the lector the impression they are full programs meant to be asembled as-is. Maybe there's where I'm wrong...

The other option I thought of was that maybe these aren't meant to be assembled as standalone programs, and that perhaps they are meant to be referenced from elsewhere instead? But again, reading further didn't enclear this anyways... it just kept bringing on more assembler listings that I couldn't compile 😬 .

Is there a recommended environment where I should run these? Should I not try assembling the listings in Chapter 2? I'd love to get any insight from other people who have read/are reading this amazing book.

Thanks in advance!

BGMP avatar Feb 06 '24 05:02 BGMP

For future readers,

After some trial and error, I managed to set up a working development environment for chapter 2! In the end, I opted for using DOSBox, plus a couple of enviornments to make it all build and link together.

Here's a step by step guide I crafted along the way:

DOSBox

Simply download DOSBox for your OS. I've left a link to their downloads page down in the references.

Once installed, mount your C drive as some local directory in your host machine. I just made a directory called DOSPROGS/ in my Desktop and that worked just fine.

Assembler (MASM611)

Get Microsoft Macro Assembler, link in the references! Then, extract the disks' content to a folder in your MS DOS C Drive. I settled for C:\DOSPROGS\MASM611\.

From DOS' command line, run the SETUP.EXE file that comes with MASM611. The installer will then prompt you with an installation screen. Follow the instructions and select all the defaults you see fit.

C

Get Borland Turbo C++, link in the references! Much like you did with MASM, extract the disks' content to a folder in your MS DOS C Drive. I used C:\DOSPROGS\TCC\.

From DOS' command line, run the INSTALL.EXE file that comes with TCC. The installer will then prompt you with an installation screen. Follow the instructions and it will install accordingly. Also, at one point in the installation process, TCC will ask you what folder it should install itself to. In my case, I selected C:\TC\.

Assembling, Compiling, Linking and Running

Step 1: Assembling

First off, you will have to write down an assembly file and use MASM to assemble it. Here's a listing taken from the book:

Listing 2.8 - An assembly procedure to set the video mode (SETMODEA.ASM)

.MODEL MEDIUM, C

.CODE

PUBLIC Set_Mode

Set_Mode PROC FAR C vmode:WORD

mov AH,0
mov AL, BYTE PTR vmode

int 10h

ret

Set_Mode ENDP

END

To assemble with MASM:

CD C:\MASM611\BIN
MASM SETMODEA.ASM;

This will create a SETMODEA.OBJ file. Keep this file around, you will have to link it later!

Step 2: Compiling

Now, you will need the C function that calls what you have defined in your ASM file. Save the following listing to a file:

Listing 2.9 - A C function to test the video mode (SETMODEC.C)

#include <stdio.h>

#define VGA256 0x13
#define TEXT_MODE 0x03

extern Set_Mode(int mode);

void main(void)
{
  // This sets the video mode to 320x200, 256 colour mode.
  Set_Mode(VGA256);

  // Wait for keyboard to be hit.
  while (!kbhit()) {}

  // Put the computer back into text mode.
  Set_Mode(TEXT_MODE);
} // end main

To compile with TCC:

CD C:\TC\BIN
TCC -IC:\TC\INCLUDE -mm -c SETMODEC.C;

This will create a SETMODEC.OBJ file. Keep this file around, you will have to link it later!

Step 3: Linking

Still from the TC directory, bring SETMODEA.OBJ into it, and link it together with SETMODEC.OBJ. You may do so as follows:

TLINK c0m SETMODEC.OBJ SETMODEA.OBJ,SETMODE.EXE,,cm -LcC:\TC\LIB

Step 4: Run!

Simply run the executable file to test the results:

SETMODE.EXE

Notes

The -mm flag passed to TCC, and the arguments c0m and cm passed to TLINK represent the type of memory model that's being used for the program. In this case, it's all ms because we're using the MEDIUM model.

Like so, you must replace these arguments to work with other models accordingly:

  • TINY: -mt; c0t and ct.
  • SMALL: -ms; c0s and cs.
  • COMPACT: -mc; c0c and cc.
  • MEDIUM: -mm; c0m and cm.
  • LARGE: -ml; c0l and cl.
  • HUGE: -mh; c0h and ch.

Well, that's it! I'll keep on reading now, hopefully this guide will help you get started.

References & Downloads

DOSBox - https://www.dosbox.com/download.php?main=1 Microsoft Macro Assembler 6.11 (3.5) (MASM611) - https://winworldpc.com/product/macro-assembler/6x Borland Turbo C++ 3.0 (3.5-720k) (TC) - https://winworldpc.com/product/turbo-c/3x

BGMP avatar Feb 08 '24 17:02 BGMP