Author Topic: why it doesn't work?  (Read 21837 times)

Offline codeferever

  • Jr. Member
  • *
  • Posts: 21
why it doesn't work?
« on: September 23, 2010, 05:54:14 AM »
why it doesn't work?
Code: [Select]
;nasm -fbin -o show.com show.asm
org 100h
mov ax, 0xb800
mov es, ax
mov di, 0

Key_wait:
mov ah, 8
int 21h

mov al,'B'
mov es:[di],al

jmp Key_wait
???

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: why it doesn't work?
« Reply #1 on: September 23, 2010, 07:37:08 AM »
I don't know why that wouldn't work - looks okay to me. You might try:

Code: [Select]
...
mov al, 'B'
mov ah, 7 ; white-on black "attribute" (color)
mov [es:di], ax ; write both character and attribute
...

Hmmm, wait - you've got "es:[di]". Nasm wants the segment inside the "[]"s, too - other assemblers like it outside. Could that be your only problem? I don't think Nasm will assemble that, as posted - "invalid combination of opcode and operands"...

If that doesn't work, there's another possibility - it may be that "fake dos", as provided by certain Windows versions, starts screen memory at some other address than 0B800:0000. There's a "query video information" interrupt that returns the "regeneration buffer" (what I usually call "screen memory") as one of its fields. I'd have to look up how to use it. I don't think you should have to do that... Try just moving the "es:" inside the "[]"s first...

Best,
Frank




Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: why it doesn't work?
« Reply #2 on: September 23, 2010, 12:08:18 PM »

Code: [Select]
mov ah, attribute 
mov al, byte to display
stosw

The stosw opcode is really what you're missing as you need to be writing the attribute, as well as incrementing register DI to point to the next write position since you are using JMP for looping.  Your code fails to initialize the attribute byte of the position you are writing to so you have no idea what's going to be shown if anything.



Offline codeferever

  • Jr. Member
  • *
  • Posts: 21
Re: why it doesn't work?
« Reply #3 on: September 24, 2010, 12:26:41 PM »
yeah,but I know only a few,without "stosw" or "lodsb"...
 ???

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: why it doesn't work?
« Reply #4 on: September 24, 2010, 02:22:45 PM »
Code: [Select]
; lodsb
mov al, [si]
inc si

;stosw
mov [es:di], ax
add di, 2

There's more to it than that, si/di can be decremented as well as incremented - really should do "cld" first to make sure the "direction flag" is "up".

Have you got the original code working? (Nasm won't assemble it, as posted)

Best,
Frank


Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: why it doesn't work?
« Reply #5 on: September 24, 2010, 03:33:44 PM »
There's more to it than that, si/di can be decremented as well as incremented - really should do "cld" first to make sure the "direction flag" is "up".

Good call, Frank.  cld or std should always be made before using any stos instruction to ensure direction.  These should be placed outside of the looping code for "normal" circumstances.

The original user may also want to add in
Code: [Select]
   %define LOOPCOUNT 10

   mov cx, LOOPCOUNT
   cld
 begin_loop:
   .
   .
   .
   stosw
   loopnz begin_loop

or, since he's writing out the same value, make use of

Code: [Select]
   %define LOOPCOUNT 10

   .
   .
   .
   mov ax, 0x742    ; attrib = 7, char = 'B' (0x42)
   mov cx, LOOPCOUNT
   cld
   repnz stosw

and then finish execution by terminating properly rather than using CTRL-C to break out of a runaway program that, in the authors original code, will eventually bomb out with an access violation or something horribly wrong.

There are many ways to skin a cat and these are but a few.  We're not going to have to cover video snow protection are we? ;D