English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 17 October 2018, 22:56   #1
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 45
Posts: 279
Using VPrintf

I'm trying to print a simple string to the shell. I get screens full of HçHçHç.. when I run the code below. Any idea what I'm doing wrong? TIA!

Code:
	SECTION TestCLI,CODE

	INCDIR 	"F:\Amiga\_MountHD\dev\include"
	INCLUDE "LVOs.i"

;-- init/startup
init:
	movem.l d1-a6,-(sp)

	move.l 	4.w,a6				;execbase (permanently open lib)
	moveq 	#0,d0 				;return value in d0

	;open dos.library
	move.l 	#dosName,a1
	jsr 	_LVOOldOpenLibrary(a6)
	move.l 	d0,dosBase

test:
	move.l 	dosBase,a6
	move.l 	#text1Format,d1
	move.l 	#text1,d2
	jsr 	_LVOVPrintf(a6)

exit:
	move.l 	4.w,a6				;execbase (permanently open lib)
	moveq 	#0,d0 				;return value in d0

	;close dos.library
	move.l 	dosBase,a1
	jsr 	_LVOCloseLibrary(a6)

	movem.l (sp)+,d1-a6

	moveq 	#0,d0 				;return value in d0
	rts

	SECTION TestCLIData,DATA
	EVEN
text1:
	dc.b 	"Just testing..",0
	EVEN
text1Format:
	dc.b 	"%s",0

	EVEN
dosName:
	dc.b 	"dos.library",0
	EVEN
dosBase:
	ds.l 	1,0
guy lateur is offline  
Old 17 October 2018, 23:16   #2
StingRay
move.l #$c0ff33,throat

StingRay's Avatar
 
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,146
You are using wrong parameters. d1 is the text to print, d2 is the formatting array.

Change your code like this:

move.l #text1,d1
move.l #text1format,d2


and text1format needs to be changed to look like this:

text1format dc.l arg1 ; ptr to argument 1

arg1 dc.b "%s",0

and your code should work. This is all untested so no guarantees or anything.
StingRay is offline  
Old 17 October 2018, 23:59   #3
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 45
Posts: 279
Quote:
Originally Posted by StingRay View Post
You are using wrong parameters. d1 is the text to print, d2 is the formatting array.
Yep, that indeed is apparently the case. The code above works as expected when I apply your changes. It's weird, though, because in the docs (http://amigadev.elowar.com/read/ADCD.../node030D.html) it says D1 is the formatting string, and D2 is a pointer to the array of formatting values (ie, values to be formatted, I assumed). Am I reading the wrong docs?

And how about decimals, btw? If I write this:
Code:
	EVEN
text1:
	dc.w 	555
	EVEN
argv11:
	dc.b 	"%d",0
	EVEN
text1Format:
	dc.l 	argv11,0
it outputs +33. Quite baffling, I must say..
guy lateur is offline  
Old 18 October 2018, 00:15   #4
Hedeon
Sonnet Hacker

 
Join Date: Mar 2012
Location: Leiden / The Netherlands
Posts: 823
that's weird. How I have learned it, is VPrintf(fmt, argv)
with fmt pointing to something like

dc.b "my address is %08lx",10

and argv pointing to the actual value (argument) you want to be formatted into that string.

so:

Code:
move.l DosBase,a6
lea fmt(pc),a1
move.l a1,d1
lea argv(pc),a1
move.l a1,d2
jmp _LVOVPrintf(a6)

fmt dc.b "register d0 = %08lx ; register d1 = %08lx",10
cnop 0,4
argv dc.l $deadbeef,12345678
At least that is how it works for VFPrintF which only adds an fh. Both values in argv will be formatted to a hexidecimal number in above example. (oh, and I like PC relative, don't mind me)

Last edited by Hedeon; 18 October 2018 at 00:36.
Hedeon is offline  
Old 18 October 2018, 00:17   #5
Hedeon
Sonnet Hacker

 
Join Date: Mar 2012
Location: Leiden / The Netherlands
Posts: 823
Quote:
Originally Posted by guy lateur View Post

And how about decimals, btw? If I write this:
Code:
	EVEN
text1:
	dc.w 	555
	EVEN
argv11:
	dc.b 	"%d",0
	EVEN
text1Format:
	dc.l 	argv11,0
it outputs +33. Quite baffling, I must say..
In my case it would be:
Code:
text1:
       dc.b "%d",0
       EVEN
text1format
       dc.w 555
with text1 being pointed to by fmt (STRPTR) and text1format being pointed to by argv (LONG*). fmt to a string with the formatting in it (like %d) the other being the values you want formatted into the string.

Last edited by Hedeon; 18 October 2018 at 00:28.
Hedeon is offline  
Old 18 October 2018, 00:39   #6
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 5,714
Quote:
Originally Posted by guy lateur View Post
Am I reading the wrong docs?
I would say you read the right docs, but you interpret it wrongly.

The first argument is the string to be printed. It may but does not need to contain format specifiers.

The second argument points to an array. Or rather a list of values. For each format specifier in the first argument, the array has to contain a corresponding value.

For %s the value must be a pointer to a string. Not the string itself.

For %d the value must be a 16bit word.

For %ld the value must be 32bit long word.

If the array does not align with the format specifiers, all values are printed wrong.

Here is a working example:
Attached Files
File Type: txt vp.asm.txt (772 Bytes, 16 views)
thomas is offline  
Old 18 October 2018, 00:44   #7
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 45
Posts: 279
Quote:
Originally Posted by Hedeon View Post
In my case it would be:
Code:
text1:
       dc.b "%d",0
       EVEN
text1format
       dc.w 555
with text1 being pointed to by fmt (STRPTR) and text1format being pointed to by argv (LONG*). fmt to a string with the formatting in it (like %d) the other being the values you want formatted into the string.
Well you're right. I must have done something wrong earlier to make me think those arguments needed to be switched. Which is quite a relief, tbh, or I would have been in for a spell in interpreting the docs from now on. So this works as expected:
Code:
test:
	move.l 	dosBase,a6
	move.l 	#text1Format,d1
	move.l 	#text1Values,d2
	jsr 	_LVOVPrintf(a6)

<snip>

	SECTION TestCLIData,DATA
	EVEN
text1Format:
	dc.b 	"%s is %d cm wide and %d cm high",0
text1TestVal:
	dc.b 	"My window",0
	EVEN
text1Values:
	dc.l  	text1TestVal
	dc.w 	288,144
guy lateur is offline  
Old 18 October 2018, 00:50   #8
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 45
Posts: 279
Quote:
Originally Posted by thomas View Post
I would say you read the right docs, but you interpret it wrongly.

The first argument is the string to be printed. It may but does not need to contain format specifiers.

The second argument points to an array. Or rather a list of values. For each format specifier in the first argument, the array has to contain a corresponding value.

For %s the value must be a pointer to a string. Not the string itself.

For %d the value must be a 16bit word.

For %ld the value must be 32bit long word.

If the array does not align with the format specifiers, all values are printed wrong.

Here is a working example:
Oops, I didn't see your post before replying myself, thomas, but thank you for your elaboration and example code, much appreciated!
guy lateur is offline  
Old 18 October 2018, 00:52   #9
Hedeon
Sonnet Hacker

 
Join Date: Mar 2012
Location: Leiden / The Netherlands
Posts: 823
Quote:
Originally Posted by guy lateur View Post
Well you're right. I must have done something wrong earlier to make me think those arguments needed to be switched. Which is quite a relief, tbh, or I would have been in for a spell in interpreting the docs from now on. So this works as expected:
Code:
test:
	move.l 	dosBase,a6
	move.l 	#text1Format,d1
	move.l 	#text1Values,d2
	jsr 	_LVOVPrintf(a6)

<snip>

	SECTION TestCLIData,DATA
	EVEN
text1Format:
	dc.b 	"%s is %d cm wide and %d cm high",0
text1TestVal:
	dc.b 	"My window",0
	EVEN
text1Values:
	dc.l  	text1TestVal
	dc.w 	288,144
Great!
Hedeon is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with VPrintf Nightfox Coders. Asm / Hardware 7 12 August 2016 17:41

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 09:02.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.
Page generated in 0.07119 seconds with 16 queries