Author Topic: Is '$$' in memory read as 0?  (Read 28674 times)

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Is '$$' in memory read as 0?
« on: December 11, 2010, 11:36:51 PM »
I was making a string print method that took kept printing out the characters in the array till it found two $ in consecutive order such as "HelloWorld$$", Its purpose is to stop printing out when it reaches the '$$' at the end. I was working on my program for a while to find what the problem was, and I think I have found it It seems that when ever it reaches the '$$' and I print it out it just prints out nothing or in other wards, looking at the ASCII table of characters is nothing. If this is the problem It does not read it as '$' and my program continues to read forever. So is this true or can you teel me what might be wrong with my code:
Code: [Select]
       mov cx, 0
        mov dx, 0
        mov di, 0

CmpString:
mov [Value + 1], si
add [Value + 1], cx
mov ax, [Value + 1]
mov bh, [String + di]
add cx, 1
add di, 1
cmp bh, '$'
jne ValCompare
add dx, 1
cmp dx, 2
je End

Value:
        db 0, 0

String:
        db "$$HelloWorld$$"

End:
In this code the string is formatted at the beginning with $$ and at the end with $$.
So what CmpString does is find where in the string the first $$ occurs and the I put that value into another method to print out the characters from that point to the next occurrence of $$.
« Last Edit: December 11, 2010, 11:42:40 PM by ThatGuy22 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Is '$$' in memory read as 0?
« Reply #1 on: December 12, 2010, 08:17:19 AM »
What?

Code: [Select]
        mov cx, 0
        mov dx, 0
        mov di, 0

CmpString:
mov [Value + 1], si

; what's in si? Baal knows!!!
; whatever it is, low byte goes in [value + 1]
; high byte scribbles on whatever's next,
; "string" (+ 0) in this case

add [Value + 1], cx

; starting with 0, this won't do any further harm.
; when cx becomes 1, "[value + 1]" becomes
; whatever was in the low byte of si... + 1.
; "[string + 0]" remains whatever was in si high byte.

mov ax, [Value + 1]

; mov si(?) + cx (0 or 1) into ax,
; which is never heard from again

mov bh, [String + di]

; start with high byte of whatever was in si
; when di =1, [string + 1] would be '$' - whoopee!

add cx, 1
add di, 1
cmp bh, '$'

; when di =1, this should be true (we never get there)

jne ValCompare

; if not, go where???
; if bh = '$':

add dx, 1
cmp dx, 2

; this will become true... if we ever make two more passes

je End

; if true, jump to "end" - which appears to
; fall off the end of your code
; if not true, attempt to execute "value" and "string",
; then fall of the end of your code.

Value:
        db 0, 0

String:
        db "$$HelloWorld$$"

End:

Dude! This "isn't even wrong"!

There's potential for confusion here. Nasm does, in fact, know $$ - outside of a quoted string - as a special symbol, "start of section" (address).

Code: [Select]
section .text
; "org 0"  is Nasm's default, if you don't say
mov ax, $$

This will put 0 in ax. But...

Code: [Select]
org 100h ; or maybe 7C00h?
section .text
mov ax, $$

now ax is 100h (or 7C00h). But as part of a quoted string, '$$' is just the two ascii characters '$' - 24h in my ascii table.

Code: [Select]
mov ax, '$$'

ax is 2424h.

Start over!

Code: [Select]
; nasm -f bin -o myfile.com myfile.asm

org 100h ; test it as a .com file

section .data ; Nasm (-f bin) will move this after "section .text"
  string0 db "$$Hello, World$$" ; this won't print anything!
  string db "Hello, World$$"  ; this should print

section .bss ; Nasm would move this after ".data"
  ; but we don't have any uninitialized data

section .text
  mov si, string
  call print_$$_terminated_string
  ret ; return to dos

print_$$_terminated_string:
; expects: pointer to string in si
; returns: nothing useful
.top:
  mov al, [si] ; or "lodsb"
  inc si          ; replaces these two instructions

  cmp al, '$'
  jne .print_char
  cmp byte [si], '$'  ; check next char
  je .done

; if not, "fall through" and print the one we've got

.print_char:
  int 29h ; or your preferred "print char in al"
  jmp .top

.done:
  ret
;-----------------------------

That's untested, and may be wrong, but it should be closer than what you've got (I hope).

Best,
Frank

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: Is '$$' in memory read as 0?
« Reply #2 on: December 12, 2010, 02:41:08 PM »
Sorry for the confusion, si is a string such as "HELLO". The si register will get compared to di, exept in di it contains "$$HELLO$$". In this case I want them to be the same.

The problem is that when I do:
Code: [Select]
mov ah, 0x0e
mov al, "address of first $"
Int 0x10
It prints out nothing.
But I may have done something wrong in my code somewhere else. I'll take another look at it.
« Last Edit: December 12, 2010, 02:43:44 PM by ThatGuy22 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Is '$$' in memory read as 0?
« Reply #3 on: December 12, 2010, 03:02:56 PM »
Okay. Be sure that "address of first $" includes a correct ds register! (common source of error!) That should work. Putting a word in [value + 1] still overwrites the first '$' in "string". Try it without that.

Best,
Frank


Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: Is '$$' in memory read as 0?
« Reply #4 on: December 12, 2010, 03:33:14 PM »
I'm not shur what you are saying but what I did us this:
Code: [Select]
String:
      db "$$HELLO$$"

mov al, [String]; [String] should give me the first byte or character.
;correct ?
mov ah, 0x0e
Int 0x10

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: Is '$$' in memory read as 0?
« Reply #5 on: December 12, 2010, 05:16:57 PM »
I have done another test with my code and It seems that it is not correctly fetching the value of $ in memory. I found this out by doing this:
Code: [Select]
PrintCharbyte
        db "$"

mov ah, 0x0e
mov al, PrintCharbyte
int 0x10
This prints out a space.

then I did this:
Code: [Select]
mov ah, 0x0e
mov al, "$"
int 0x10
And It worked it printed out $
but what makes the first one not work?

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Is '$$' in memory read as 0?
« Reply #6 on: December 12, 2010, 05:54:08 PM »

Offline cm

  • Jr. Member
  • *
  • Posts: 65
Re: Is '$$' in memory read as 0?
« Reply #7 on: December 12, 2010, 06:12:18 PM »
I have done another test with my code and It seems that it is not correctly fetching the value of $ in memory. I found this out by doing this:
Code: [Select]
PrintCharbyte
        db "$"

mov ah, 0x0e
mov al, PrintCharbyte
int 0x10
This prints out a space.

Try [PrintCharbyte], which would print out the dollar sign that you stored there (if ds is set correctly). Int10.0E takes a single byte/character value as input in al - not an address.

If I understand your assignment correctly, this (DOS flat-form .COM) program should do it:

Code: [Select]
org 100h

start:
 mov si, inputstring
 call count_til_double_dollar
 add si, ax
 inc si
 inc si
 call count_til_double_dollar
 mov cx, ax
 jcxz .end
.loop:
 lodsb
 ; note that you could use Int29 or Int21.02 instead for displaying here
 ; but they do the same if you don't redirect the output and use no escape sequences
 mov ah, 0Eh
 mov bx, 7
 int 10h
 loop .loop
.end:
 mov ax, 4C00h
 int 21h

count_til_double_dollar:
 push si
.loop:
 lodsw
 dec si
 cmp ax, "$$"
 jne .loop
 dec si
 pop ax
 xchg ax, si
 sub ax, si
 retn

inputstring:
 db "foo$$bar$$"


This displays "bar". You could remove the "foo" string here and it would still do the same. You could also replace the "bar" string by any other string to display (unless it contains a double-dollar-sign sequence). I left figuring out how it works as an exercise to you (actually I'm also too lazy to comment it).

Frank, even though the dollar sign is used by NASM to get the current section's offsets, the usage here is completely unrelated to that.
C. Masloch

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: Is '$$' in memory read as 0?
« Reply #8 on: December 12, 2010, 07:41:08 PM »
Sorry I meant for it to be "mov al, [PrintChartbyte]", I must have written it down wrong. I should have copied and pasted it.
 -But it still didn't work

I see how your code works but what I don't understand is why this didn't work:
Code: [Select]
PrintCharbyte
        db "$"

mov ah, 0x0e
mov al, [PrintCharbyte]
int 0x10
« Last Edit: December 12, 2010, 07:45:56 PM by ThatGuy22 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Is '$$' in memory read as 0?
« Reply #9 on: December 12, 2010, 07:50:26 PM »
Where is ds?

And, as Christian suggests,

Code: [Select]
  mov bx, 7

Won't hurt, either.

Best,
Frank


Offline cm

  • Jr. Member
  • *
  • Posts: 65
Re: Is '$$' in memory read as 0?
« Reply #10 on: December 12, 2010, 08:54:10 PM »
I should have copied and pasted it.

Yes that would be most appropriate.

Code: [Select]
PrintCharbyte
        db "$"

mov ah, 0x0e
mov al, [PrintCharbyte]
int 0x10

Please show me a small test case - but in an entire file - and the NASM command line that you use to compile it. Also try "mov bx, 7" in front of the Int10 instruction.
C. Masloch

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: Is '$$' in memory read as 0?
« Reply #11 on: December 13, 2010, 12:37:48 AM »
DS is where where it is by default(I don't set it).