Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Sound

I'm really, really new to assembly. So, can anyone teach me how to play any sound using turbo assembly. My prof tells us that we can be able to do this, but he can't teach us how beacuse of time constraints. Just curious.

Comments

  • bono02bono02 Member Posts: 2
    : I'm really, really new to assembly. So, can anyone teach me how to play any sound using turbo assembly. My prof tells us that we can be able to do this, but he can't teach us how beacuse of time constraints. Just curious.
    :


    >> You can try searh on the net :)
    Well basically sound uses port 61h, it ins an outs to that port.


  • blipblip Member Posts: 756
    Here's a cheap snippet off the top of my head (not tested):
    [code]
    ;Assemble into a .COM file using NASM.
    ;This program takes a Hertzian frequency specified on the command line
    ;and plays it over the internal speaker until the user presses a key.
    EnableSpeaker equ 00000011b
    DisableSpeaker equ 11111100b
    CommandLine equ 81h
    PIT_Frequency equ 1193182 ;Hz
    SetupPIT equ 00110110b

    Start:
    mov si,CommandLine

    ;This takes in a decimal number in ASCII pointed to by DS:SI and
    ;converts it to hex so that it can be processed.
    ConvertASCIIDecToHex:
    cld ;Make string instructions work in a forwards direction.
    mov bp,10 ;Destination base.
    xor bx,bx ;This will hold the hex number as it comes in.

    ConvertLoop:
    lodsb
    xor dx,dx ;No numbers above 65535 are up for conversion (used in MUL).
    cmp al,"0"
    jb PlayFrequency
    cmp al,"9"
    ja PlayFrequency
    sub al,"0" ;By doing this, the ASCII digit will be converted into a useable number.
    movzx cx,al ;Same as XOR CH,CH and MOV CL,AL.
    mov ax,bx
    mul bp ;Make room for the new digit by doing DX:AX = AX * BP
    add ax,cx ;Add in the digit.
    mov bx,ax ;Save the result into the "container".
    test dx,dx ;This checks if DX is zero or not.
    jz ConvertLoop ;If not, the number is too large. If so, loop.

    IncorrectNumberRange:
    mov ah,9
    mov dx,WrongNum
    int 21h
    int 20h

    WrongNum db "The input frequency entered on the command line must be between 65536 and 0!$"

    PlayFrequency: ;Can't do it just yet. Need to do a calculation first.
    test bx,bx ;A divide by zero would not be good!
    jz IncorrectNumberRange

    in al,61h
    or al,EnableSpeaker
    out 61h,al
    mov al,SetupPIT ;Set up the PIT for what's being done.
    out 43h,al

    mov dx,(PIT_Frequency>>16) ;Get high word into DX.
    mov ax,(PIT_Frequency & ((1<<16)-1)) ;Put low word into AX.
    div bx ;AX r DX = DX:AX / BX
    ;You could round the frequency, but I'm not going to.

    out 42h,al ;Send the LSB.
    mov al,ah
    out 42h,al ;Then the MSB.

    xor ah,ah ;Wait for a keypress before continuing (the key will simply
    int 16h ;be discarded).

    in al,61h
    and al,DisableSpeaker
    out 61h,al

    int 20h
    [/code]
Sign In or Register to comment.