Author Topic: (preprocessor) something wrong with divisions  (Read 19235 times)

TheClue/Nash of Subsystems

  • Guest
(preprocessor) something wrong with divisions
« on: August 05, 2007, 04:52:12 PM »
hello to all! i'm new on NASM and asm and i'm learning it for my bachelor thesis so...i'll probably annoy a bit with stupid questions at starts, i'm afraid :)

so, here's the first. i'm working on some sample code to build a .com (dos16) file. But this code:

mov sp, END + 0x12
mov ah, 0x4A
mov bx, (END/16) + 0x04
int 0x21

(it should resize the memory allocated to my dummy com program. END is a label stucked at the very end of file)

well the first mov correctly shift the stack point to...uhm...somewhere near to me :)
but the third mov gives me this error

"division operator may only be applied to scalar values"

i suppose END/16 + 0x04 is computed at assemble time so...whats wrong?

and additionaly i get this error

"phase error detected at end of assembly"

can someone explain me where i went wrong and what those error messages mean?

tnx in advance!

theclue

nobody

  • Guest
Re: (preprocessor) something wrong with divisions
« Reply #1 on: August 05, 2007, 06:51:50 PM »
Use (END - $$) and I think you'll find that it works (in the same section - in multiple sections you may have to calculate sizes of the sections seperately and add 'em up).

"scalar" is found only in the error messages - you won't find it in the manual. It means that a label like "END" is a relocatable value - its value isn't known until link-time. Since Nasm does its own "linking" with "-f bin", it *could* theoretically be figured out, but would require some special-case code. It probably isn't the value you want, anyway. Assuming "org 100h" for a .com file, "END" is going to evaluate to 123h or so - not the size of your code, which is what you want to divide by 16. The difference between two labels is "scalar", a single label is not. I've been known to use labels "aaa:" and "zzz:" to "clearly" indicate beginning and end of code. ("$$" just means "beginning of section", but may be a little cryptic and unreadable).

The "phase error" is a bonus error message. :) You're *supposed* to see it if Nasm can't resolve all symbols in the number of passes you specify with the "-O" switch, but it crops up if almost anything goes wrong. Ignore it, unless it's the *only* error message, in which case increase the parameter to "-O" (I like "-O999").

In real mode, interrupts (like the timer interrupt) use *your* stack. I'm not sure what we can expect for maximum stack usage... 0x12 might be a little light. (unless you need every last byte, resizing to 64k and leaving the stack alone is easy)

I'm not sure what the extra 4 paragraphs you allocate are for... "/16" or ">>4" truncate, so you might want to add 1... but that might be a "waste"... (zzz - aaa + 15)/16 should get it right. Plus any extra you need...

(Do you know that the US Post Office considers "batchelor's degree" to be sexist language, and won't let employees use it?)

Best,
Frank

TheClue/Nash of Subsystems

  • Guest
Re: (preprocessor) something wrong with divisions
« Reply #2 on: August 08, 2007, 08:45:05 AM »
tnx for the great reply! everything is clear as fresh air, now! :)

1. i did not considered that END could be relocatable. Tnx a lot! You saved me from having the same error the day I'll switch to EXE! :) So (END - START)/16 + 1 should be safe, instead?

(meanwhile i used a workaround that is very strange; mov bx, END | shr bx, 4 | inc bx and now i don't know WHY it works, instead :))

2. typo. the increment was of 0x01 not 0x04 (just one for the truncation)

3. it was only an efford to make an "hello, world" with the minimum minimum minimum minimum amount of memory needed. so only one interrupt call was needed and only a smaaaaal stack, too. just a test :)

4. well consider that in my country the world "bachelor" is meaningless, but u have to use it to let ppl understand on which university path u are talking about so....damned slang ;)

tnx again and have a nice summer

gabrio