11 Replies - 65529 Views - Last Post: 10 December 2010 - 03:52 PM

#1 ljfox4   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 10-February 07

Generating Sound in Assembly

Posted 14 February 2007 - 11:01 PM

I have checked google, but I can't seem to find any resources on generating sound and making general noise in x86 assembly. Does anyone know of any good resources?
Is This A Good Question/Topic? 0
  • +

Replies To: Generating Sound in Assembly

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Generating Sound in Assembly

Posted 19 February 2007 - 10:20 PM

Well some questions first:

What kind of sound are you looking for?
I have seen examples that just generate tones or warbles on the PC Speaker. Most of these are given in examples of programming the 8253 and 8255A PPI.


If you are talking about sound card sound...
What environment are you in, DOS, WINDOWS, LINUX?
Are you talking SoundBlaster compatable sound card?
If you are in DOS you will probably want to use the drivers CT-VOICE.DRV
  • Find the file.
  • Allocate a block of memory to hold the driver (16 bytes larder then the file size).
  • copy CT-VOICE.DRV into memory.
  • Make Far calls to the functions, BX holds function number.
or CTVDSK.DRV

You can program the Sound Blaster at hardwear level:

Some DSP info

Good Guilde To SoundBlaster hardware

IF you are in windows you will need to get into the MultiMedia API.

Most of the info you will find will be in C or C++, though I have found both basic and pascal examples, so you will have to do a little translation here and there but I am sure you can find a sample driver written in ASM.

I have a book "Assembly Language Master Class" which has an example sound driver written in assembly. Problem is that this book is pretty old and does not touch on any windows related issues.

I hope that this was helpful.

This post has been edited by NickDMax: 20 February 2007 - 04:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Generating Sound in Assembly

Posted 19 February 2007 - 10:43 PM

Here is a basic "beep" sound in asm:

Beep	 PROC USES AX BX CX
	IN AL, 61h  ;Save state
	PUSH AX	  

	MOV BX, 6818; 1193180/175
	MOV AL, 6Bh  ; Select Channel 2, write LSB/BSB mode 3
	OUT 43h, AL	 
	MOV AX, BX	
	OUT 24h, AL  ; Send the LSB
	MOV AL, AH	 
	OUT 42h, AL  ; Send the MSB
	IN AL, 61h	 ; Get the 8255 Port Contence
	OR AL, 3h		
	OUT 61h, AL  ;End able speaker and use clock channel 2 for input
	MOV CX, 03h ; High order wait value
	MOV DX 0D04h; Low order wait value
	MOV AX, 86h;Wait service
	INT 15h			
	POP AX;restore Speaker state
	OUT 61h, AL
	RET
BEEP ENDP


This code was derived from code in Using Assembly Language 3rd Edition by Allen L Wyatt St.

I belive that you need an actual PC Speaker to use this, I don't think that the sound card will catch this.

This post has been edited by NickDMax: 19 February 2007 - 10:45 PM

Was This Post Helpful? 1
  • +
  • -

#4 ljfox4   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 10-February 07

Re: Generating Sound in Assembly

Posted 20 February 2007 - 01:40 PM

Thank you Nick. What I was looking for is just PC speaker sounds, not so much sound card stuff. Thank you very much for the example code provided, I think it is just what I am looking for(That being something that makes noise that I can play around with).
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Generating Sound in Assembly

Posted 20 February 2007 - 04:30 PM

If you need more examples google for 8253 and 8255A and sound.
Was This Post Helpful? 0
  • +
  • -

#6 Blatin26   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-February 07

Re: Generating Sound in Assembly

Posted 24 February 2007 - 09:55 PM

Hi all,
I need help Generatint the first 21 members of the Fibonacci sequence, store them in Memory, and use Dump Memory
to display the sequence I am having the hardiest time..

JB
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Generating Sound in Assembly

Posted 25 February 2007 - 04:52 AM

??? And this pertains to generating sound in assembly how?

To generate Fibonacci just add the last two numbers to get the current, the first two are 1.

F(n) := if n=1 or 2 return 1 else return F(n-1)+F(n-2)

There are faster methods that you can find in any number theory book (and just about any intoduction to progamming book).

Other than that, this post is in the wrong place.
Was This Post Helpful? 0
  • +
  • -

#8 Simo_fcis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-April 08

Re: Generating Sound in Assembly

Posted 19 November 2008 - 01:48 PM

Hey everyone,

I tried the code posted above for generating sound in Assembly but it won't run.

For the Assembly course I'm taking, I use Visual Studio with Irvine Library.

The code builds just fine but after I run it, it freezes and windows gives me an error :
"Project.exe has stopped working".

When I tried debugging instead of just running, it stops at this instruction :

IN AL, 61h ;Save state


and tells me

Quote

Unhandled exception at 0x0040102f in Project.exe: 0xC0000096: Privileged instruction


Can someone please help me out with this?

I have a project in this course and one of the options for the project is making a "piano"...but this is the only thing I found about generating sound in assembly so I figured its a start...but if anyone knows how I can generate different musical notes that would be great too ;-)

Thank You.
Was This Post Helpful? 0
  • +
  • -

#9 jeff666   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 193
  • Joined: 30-December 08

Re: Generating Sound in Assembly

Posted 30 December 2008 - 06:32 AM

Just use Beep() api on Win32

This post has been edited by nathanpc: 10 December 2010 - 03:36 PM
Reason for edit:: Added the [il] tags

Was This Post Helpful? 0
  • +
  • -

#10 Guest_qwertyforever*


Reputation:

Re: Generating Sound in Assembly

Posted 24 March 2010 - 11:16 AM

on the code above, change
OUT 24h, AL  ; Send the LSB

to
OUT 42h, AL  ; Send the LSB

it should work then...

This post has been edited by nathanpc: 10 December 2010 - 03:37 PM
Reason for edit:: Added the [code] tags

Was This Post Helpful? 0

#11 Guest_OOEngineerOO*


Reputation:

Re: Generating Sound in Assembly

Posted 10 December 2010 - 05:36 AM

hello
thank you NickDMax very much and i googled the 8253 and i understood how to generate a sound ,but i think there is something wrong,i dont hear any sound ,my laptop is acer aspire 5050 ,does the port number defer ??

here are the code that i used
 org 100h

MOV     DX,2000          ; Number of times to repeat whole routine.

MOV     BX,1             ; Frequency value.

MOV     AL, 10110110B    ; The Magic Number (use this binary number only)
OUT     43H, AL          ; Send it to the initializing port 43H Timer 2.

NEXT_FREQUENCY:          ; This is were we will jump back to 2000 times.

MOV     AX, BX           ; Move our Frequency value into AX.

OUT     42H, AL          ; Send LSB to port 42H.
MOV     AL, AH           ; Move MSB into AL  
OUT     42H, AL          ; Send MSB to port 42H.

IN      AL, 61H          ; Get current value of port 61H.
OR      AL, 00000011B    ; OR AL to this value, forcing first two bits high.
OUT     61H, AL          ; Copy it to port 61H of the PPI Chip
                         ; to turn ON the speaker.

MOV     CX, 100          ; Repeat loop 100 times
DELAY_LOOP:              ; Here is where we loop back too.
LOOP    DELAY_LOOP       ; Jump repeatedly to DELAY_LOOP until CX = 0


INC     BX               ; Incrementing the value of BX lowers 
                         ; the frequency each time we repeat the
                         ; whole routine

DEC     DX               ; Decrement repeat routine count

CMP     DX, 0            ; Is DX (repeat count) = to 0
JNZ     NEXT_FREQUENCY   ; If not jump to NEXT_FREQUENCY
                         ; and do whole routine again.

                         ; Else DX = 0 time to turn speaker OFF

IN      AL,61H           ; Get current value of port 61H.
AND     AL,11111100B     ; AND AL to this value, forcing first two bits low.
OUT     61H,AL           ; Copy it to port 61H of the PPI Chip
                         ; to


 hlt


and thank you all :)

This post has been edited by ishkabible: 10 December 2010 - 10:48 AM
Reason for edit:: please use code, also this thread is dead please dont comment on it

Was This Post Helpful? 0

#12 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Generating Sound in Assembly

Posted 10 December 2010 - 03:52 PM

Just to introduce more useful knowledge here on this thread. Remember that things like song, depends a lot of which platform you are and which hardware you have. Taking as example 6202 on a NES platform, there is a very nice tutorial about sounds/songs at Day 14 - Sound Channels 1-3
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1