My CS320 project, IP subnet calculator in VB6, i believe there are better ways of doing this project, i just don't know how.
Today, I and Marlon started our thesis programming together. We were able to now record and play audio. Thanks to our joined power! hehe:) We were able to fix the code by creating a separate thread for recording…Today, we also learned about the different audio formats…
package record;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.control.*;
import javax.microedition.media.*;
import java.io.*;
/**
* This form shows how the cellphone’s microphone can be used to record audio and how the recorded audio can be played on the cellphone. The original author of this code is Alexander De Luca. There are some problems regarding possible deadlock when recording audio. As a solution, we opted to create another thread just handling the recording of audio.
* modified on November 18, 2006
*/
public class RecordForm extends Form implements CommandListener{
private StringItem messageItem;
private StringItem errorItem;
private final Command recordCommand, playCommand;
private Player p;
private byte[] recordedSoundArray = null;
public RecordForm() {
super(”Record Audio”);
messageItem = new StringItem(”Record”, “Click record to start recording.”);
this.append(messageItem);
errorItem = new StringItem(”", “”);
this.append(errorItem);
recordCommand = new Command(”Record”, Command.SCREEN, 1);
this.addCommand(recordCommand);
playCommand = new Command(”Play”, Command.SCREEN, 2);
this.addCommand(playCommand);
StringBuffer inhalt = new StringBuffer();
this.setCommandListener(this);
}
public void commandAction(Command comm, Displayable disp) {
if(comm==recordCommand) {
try {
Record record1= new Record(); //thread
record1.start();
} catch(Exception e){
System.err.println(e);
}
} else if(comm == playCommand) {
try {
ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedSoundArray);
Player p2 = Manager.createPlayer(recordedInputStream,”audio/basic”);
p2.prefetch();
p2.start();
} catch (IOException ioe) {
errorItem.setLabel(”Error”);
errorItem.setText(ioe.toString());
} catch (MediaException me) {
errorItem.setLabel(”Error”);
errorItem.setText(me.toString());
}
}
}
public class Record extends Thread
{
private boolean quit=false;
public void run ()
{
while (!quit)
{
try
{
// create the Player object using amr encoding
p=Manager.createPlayer(”capture://audio?encoding=pcm”);
p.realize();
// get the RecordControl over this Player
RecordControl rc = (RecordControl)p.getControl(”RecordControl”);
// create an OutputStream which the RecordControl will use
// to write write the recorded data.
ByteArrayOutputStream output = new ByteArrayOutputStream();
rc.setRecordStream(output);
// start the recording
rc.startRecord();
p.start();
messageItem.setText(”recording…”);
Thread.currentThread().sleep(5000); //record for 5 seconds
messageItem.setText(”done!”);
rc.commit();
// save the recordedData in a byte array
recordedSoundArray = output.toByteArray();
// close the player
p.close();
quit=true;
} catch (IOException ioe) {
errorItem.setLabel(”Error”);
errorItem.setText(ioe.toString());
} catch (MediaException me) {
errorItem.setLabel(”Error”);
errorItem.setText(me.toString());
} catch (InterruptedException ie) {
errorItem.setLabel(”Error”);
errorItem.setText(ie.toString());
}
}
}
}
}
CS 217 – ‘Computer Architecture with Assembly Programming’, hearing this course description gave me frights. I didn’t have even a little confidence that I could be receptive to lessons before that shall be taken in the entire semester, but I was able to cope with the pace as we progressed.. Truly this course is difficult but I had a lot of fun learning assembly language. It is mainly because it was fun learning with teacher and classmates rather than alone. And what makes assembly programming fun is because succeeding to do so gives me a sense of accomplishment. During our last meeting, we were planning to possibly have a group assembly learning during the next sem… possibly dwelling on OS stuffs. The idea was actually brilliant because this coming sem we will have OS class, and an application such as OS programming will really help me understand OS all the more. I also want to learn how to debug. I wish to master assembly programming, however I need to remain idle for the mean time to give way for our thesis. Our thesis is much more important as for this moment. Assembly could wait, I just hope I won’t lose the enthusiasm when I’m ready to continue.
As an end activity in this class, we were assigned to do our own editor. This is hard… and my code was really messy… If it were not because experts help me to improve my original code…I really doubt I would be posting the code here. Anyways, here it is though improvements can still be made.
This is our CS 315 final project. We were asked to do some simple OpenGL program incorporating projection, illumination, transformation, and the like in C++. I and my partner JC, chose to improve Mr. So’s clock program. We added more commands and capabilities such that it would really function like a real clock. When our program runs, our clock’s time is set according to system time. We also added some ticks and beep sound
Too much for the introduction, just run and see how we did it….
This program beeps every 15 seconds unless you exit command prompt. I enjoyed running this, even adjusting the time and made it beep faster. People at home were worried and at the same time amazed of what i have done. Though i just read about this and revised a little. Try running this and enjoy!
;——————–initialize tsr———————-
init:
cli ; prevent further interrupts
mov ah, 35h ; get the current CS:IP
mov al, 08h ; of int 08
int 21h ; result to ES:BX
mov word ptr oldint8, bx ; save BX
mov word ptr oldint8+2, es ; save ES
mov ah, 25h ; set CS:IP
mov al, 08h ; for the new INT 08
mov dx, offset routine ; DX=offset IP,
int 21h ; DS=CS set by COM
mov ah, 31h ; request stay resident
mov dx, offset init ; set size of resident portion
sti ; restore interrupts
int 21h
oldint8 dd ?
count dw 275 ; 275 X 54.94 ms = 15 seconds
;———————–resident program—————————
routine:
dec cs: count ; is the time up?
jnz exit
mov cs: count, 275 ; if yes initialize the count
mov ah, 0eh ; and
mov al, 7 ; beep the speaker
int 10h
exit:
jmp cs: oldint8 ; take care of INT 08
/*
* OpenGL implementation of Warns illumination model(spotlight)
* for better understanding read Computer Graphics by Hill
* page 426
*/
#include <GL/glut.h>
void init(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 0.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
//set spotlight’s position
GLfloat dir_light[]={2.0, 1.0, -4.0};
GLfloat light_position[] = { 2.0, 1.0, 5.0, 1.0 };
glClearColor (1.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
/*
* a cutoff angle of 45 degrees
* default value is 180 degrees
*/
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF, 45.0);
/*
* The desired falloff of light with angle(E) is 1
* As E increases the spotlight disappears
* default value is 0
*/
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT, 1.0);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,dir_light);
//enables light
glEnable(GL_LIGHTING);
//enables this particular source.
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere (1.0, 20, 16);
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h, 1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (”Single-Point Light Source”);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
/*
* This is Phong illumination model, an illumination model for
* non-perfect reflectors.This is the most popular illumination model
* although does not actually explain the real light source.
*/
#include <GL/glut.h>
void init(void)
{
GLfloat mat_shininess[] = { 10.0 };
//set light position
GLfloat light_position[] = { 1.0, 1.0, 2.0, 1.0 };
//define colors for ambient, diffuse, and specular light
GLfloat light_Ka[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_Kd[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_Ks[] = { 1.0, 1.0, 1.0, 1.0 };
glClearColor (1.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
/*GL_LIGHT0 means there is only one light source
* we can actually have maximum of 8 light sources
* denoted as GL_LIGHT0, GL_LIGHT1,…,GL_LIGHT7
*/
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//attach defined lights to light source
glLightfv(GL_LIGHT0, GL_AMBIENT, light_Ka);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_Kd);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_Ks);
glMaterialfv(GL_FRONT, GL_SPECULAR, light_Ks);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//enables light
glEnable(GL_LIGHTING);
//enables this particular source.
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere (1.0, 20, 16);
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h, 1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (”Single-Point Light Source”);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
;———————–get system time—————————
mov ah, 02ch
int 21h
;————————get hour (00-24)————————
xor ax, ax
xor dx, dx
mov al, ch
call display
mov dl,’:’
int 21h
;————————–get minute—————————
xor ax, ax
xor dx,dx
mov al, cl
call display
;—generate 10 3-digit “random” numbers——
mov ch, 10
num:
xor ax, ax
xor bx, bx
mov dh, dl
;————————3 digit numbers————————-
mov cl,3
digits:
mov al, dl ; get millisec
mov bl, 10
div bl
add ah, 30h
mov dl, ah
mov ah, 02
int 21h
shr dl,1 ; digits seem to random
dec cl
jnz digits
mov ah,02 ; go to nxt line
mov dl,0ah
int 21h
mov dl,0dh
int 21h
mov dl, dh ; generate more 3-digit numbers
add dl, 17
dec ch
jnz num
int 20h
;————–display hour/minute———————
display:
mov bl, 10
div bl
add al, 30h
add ah, 30h
mov dh, ah
mov ah, 02
mov dl, al
int 21h
mov dl,dh
int 21h
ret
Oh my God! I just can’t help not posting what I feel…I think I made it! If I’m not mistaken, I was able to connect two Bluetooth devices and communicate with each other through the RFCOMM serial protocol. I run it in the Java Wireless Toolkit because my PC could not even create new J2ME application in NetBeans. And I wish to test this module in a real device.
Here’s the result:
Build Result:
Project “BluetoothMidlet” loaded
Project settings saved
Building “BluetoothMidlet”
Build complete
Server side:
Running with storage root DefaultColorPhone
Running with locale: English_United States.1252
Waiting for connection…
Received from Client: INFO
Waiting for connection…
Execution completed.
1312245 bytecodes executed
27107 thread switches
896 classes in the system (including system classes)
5922 dynamic objects allocated (192736 bytes)
4 garbage collections (118500 bytes collected)
Client side:
Running with storage root temp.DefaultColorPhone1156995903898
Running with locale: English_United States.1252
deviceDiscovered()
Address: 0123456789AF
Major Device Class: 512
Minor Device Class: 4
Friendly Name: WirelessToolkit
inquiryCompleted()
servicesDiscovered()
SERVICE_SEARCH_COMPLETED
Service URL: btspp://0123456789AF:1;master=false;encrypt=false;authenticate=false
InfoServer: Your command was: INFO
Execution completed.
1102515 bytecodes executed
3463 thread switches
897 classes in the system (including system classes)
5892 dynamic objects allocated (191284 bytes)
5 garbage collections (132372 bytes collected)
Thanks to Miss Ebony Domingo for the J2ME tutorials and encouragement and to Niel for helping me gather resources.
To Jc and Marlon…Wish to test this on real devices soon! I wanna know if it could stream voice through RFCOMM. Cheers!
Someone guess what this code do…:) This is an OpenGL code in C++
#include <GL/glut.h>
void event_display (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0);
glRotatef(45.0, 0.0, 0.0, 1.0);
glRotatef(-45.0, 0.0, 1.0, 0.0);
glRotatef(90.0, 1.0, 0.0, 0.0);
glRotatef(45.0, 0.0, 1.0, 0.0);
glRotatef(-45.0, 0.0, 0.0, 1.0);
glutWireCube(.5);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 1.0);
glEnd();
glFlush();
}
void init (void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(event_display);
glutMainLoop();
}
lea si, input
xor cx,cx
mov cl, 4
check:
mov ah, 00
int 16h
cmp al, ‘0′
jb check
cmp al, ‘1′
ja check
mov [si], al
inc si
loop check
call convert
int 20h
input db 5 dup (’$')
;——————–convert——————–
convert:
dec si
mov ah, 09
lea dx, input
int 21h
mov ah, 02h
mov dl, 0ah
int 21h
mov dl, 0dh
int 21h
xor bx,bx
mov cl,0
xxx:
mov al, [si] ; al contains character not value
sub al,30h ; convert to value
shl al, cl
add bl,al
dec si
inc cl
cmp cl,4
jne xxx
add bl,30h ; 0-9
cmp bl,’9′
jbe print
add bl, 7h ; A-F
print:
mov ah, 02
mov dl,bl
int 21h
ret
This is a shorter and lazier version of hex to binary conversion. It displays right away the binary equivalent of a valid hexadecimal and waits user to press enter key to end. Thanks to sir for the convert algo…hehe:)
check:
mov ah, 00
int 16h
cmp ah, 1ch ; scan key for enter key
je exit
cmp al, ‘0′
jb check
cmp al, ‘9′
ja ucase
sub al, 30h
call convert
jmp check
ucase:
cmp al, ‘A’
jb check
cmp al, ‘F’
ja lcase
sub al, 37h
call convert
jmp check
lcase:
cmp al, ‘a’
jb check
cmp al, ‘f’
ja check
sub al, 57h
call convert
jmp check
exit:
int 20h
loop check
;———————–convert——————————
convert:
mov ch, 4 ; 4 bits
mov cl, 3
mov bl, al
xxx:
mov al, bl
shr al, cl
and al, 01 ; bit masking- isolate least bit 0
add al, 30h
mov ah, 02 ; display binary equivalent
mov dl, al
int 21h
dec cl
dec ch
jnz xxx
mov dl, ‘ ‘
int 21h
ret
Well…i posted this code na although it is not yet perfect.
If number of vowels/consonants is 10 or more, it will display it’s ascii equivalent not the number. I shall post the update soon or perhaps you want to help me with it…hehe:)
mov ah, 0ah
lea dx, parameter
int 21h
mov ah, 02h
mov dl, 0ah
int 21h
mov dl, 0dh
int 21h
;—————–initializtions—————–
xor bx,bx ; count vowels
xor dl, dl ; count consonants
lea si, input ; load string
xor cx, cx
mov cl, [actlen] ; loop counter
;———————loop———————–
; idea: filter out lot letters
; check if vowels. if yes, update vowel counter
; if no, update consonant counter
;————————————————
voc:
mov al, [si]
cmp al, 122 ; filter not letters
ja skip
cmp al, 96
ja check
cmp al, 90
ja skip
cmp al, 65
jb skip
check:
cmp al, ‘A’
je vowel
cmp al, ‘a’
je vowel
cmp al, ‘E’
je vowel
cmp al, ‘e’
je vowel
cmp al, ‘I’
je vowel
cmp al, ‘i’
je vowel
cmp al, ‘O’
je vowel
cmp al, ‘o’
je vowel
cmp al, ‘U’
je vowel
cmp al, ‘u’
je vowel
jne cons
vowel:
inc bl
jmp skip
cons:
inc dl
jmp skip
skip:
inc si
loop voc
;—————-end of loop————————-
;so that bl becomes character
add bl, 48
add dl, 48
;———————–results———————-
; first is the consonant count
; followed by vowel count
;—————————————————-
mov ah, 02h
mov dl, dl
int 21h
mov dl,0ah
int 21h
mov dl,0dh
int 21h
mov dl, bl
int 21h
int 20h
parameter label byte
maxlen db 20
actlen db ?
input db 20 dup (’$')
; convert small letters to capital AND vice-versa
mov ah,0ah
lea dx,parameter
int 21h
mov ah,02
mov dl,0ah
int 21h
mov dl,0dh
int 21h
; initialize
lea si, input
xor cx, cx
mov cl, [actlen]
; We wish to check if character is a letter
; if yes, capitalize small letters and vice-versa
; if no, copy character
invert:
mov al, [si]
cmp al, 122
ja skip
cmp al, 96
ja capital
cmp al, 90
ja skip
cmp al, 63
jb skip
add al, 32
jmp store
capital:
sub al, 32
store:
mov [si], al
skip:
inc si
loop invert
mov ah, 09
lea dx, input
int 21h
int 20h
parameter label byte
maxlen db 0FF
actlen db ?
input db 0FF dup(’$')
; reverse string
mov ah,0ah
lea dx,parameter
int 21h
mov ah,02
mov dl,0ah
int 21h
mov dl,0dh
int 21h
; initialize
xor cx, cx ; zero out cx
lea si, origstring ; si = inputted string
mov cl, [actlen] ; cl = length of inputted string
dec cl ; since string starts from 0 to cl-1
lea di, revstring ; di = empty
add di, cx
reverse:
mov al, [si] ; get character
mov [di], al ; copy to di at ‘cl-1′th position
dec di
inc si
cmp al, ‘$’ ; means end of string
jne reverse
mov ah, 09
lea dx, revstring
int 21h
int 20h
parameter label byte
maxlen db 0FF
actlen db ?
origstring db 0FF dup(’$')
revstring db 0FF dup (’$')
At last! This head-breaking palindrome program is done…
; –ask string from user–
mov ah,0ah
lea dx,parameter
int 21h
mov ah,02
mov dl,0ah
int 21h
mov dl,0dh
int 21h
;–intialize–
mov cx, 0 ; or xor cx, cx –for making cx=0
lea si, namefield ; load address of namespace
mov cl, [actlen] ; cl refers to the length of the string
dec cl ; strings start at 0 and ends at [actlen]-1
lea di, namefield ; load address of namespace
add di, cx ; di now points to the last character of string
mov ah, 09 ; print string function
compare:
mov cl, [si] ; get character from si
mov ch, [di] ; get character from di
cmp cl, ch ; compare two characters
jne stop ; if not equal, not palindrome
inc si
dec di
cmp di, si ; check if si>di
jns compare
lea dx, pal
int 21h
jmp exit
stop:
lea dx, notpal
int 21h
exit:
int 20h
parameter label byte
maxlen db 0FF
actlen db ?
namefield db 0FF dup(’$')
pal db ‘This string is a palindrome$’
notpal db ‘This string is not a palindrome$’
lea si, msg ; ds:si points to message
mov cx, 7 ; cx holds message length
toupper:
mov al, [si] ; al holds current letter
cmp al, 97
jb skip
cmp al, 122
ja skip
sub al, 32 ; capitalize letter
mov [si], al ; replace letter
skip:
inc si ; move to next letter
loop toupper ; loop 7 times
mov ah, 09 ; print function
lea dx, MSG ; load string to be printed
int 21h ; call DOS
int 20h ; exit to DOS
MSG DB “GraInNe$” ; define message
For a little JAVA tutorial, I made this site for you…
http://www.geocities.com/phengpheng15/index.html
MSG DB ‘Yap$’
MSG2 DB ‘Grainne$’
MOV CX, 3
LEA si, MSG
LEA di, MSG2
recur:
MOV AL, [si]
MOV [di], AL
INC si
INC di
DEC CX
JNZ recur
mov al, ‘$’
MOV AH, 09
LEA DX, MSG2
INT 21H
INT 20H
Another way of doing this code at the same time conscious about memory usage daw[coded by Niel]
MSG DB ‘Yap$’
MSG2 DB ‘Grainne$’
mov cx, 3 ; counter for REP instruction
lea si, MSG ; string to be copied
lea di, MSG2 ; where to copy
cld ; clear directrion flag
rep movsb ; store byte from si to di
mov al, ‘$’ ; the NULL terminator. remember?
mov [di], al ; terminate the new string
mov ah, 09 ; print function
lea dx, MSG2 ; string to be printed
int 21h ; call DOS
int 20h ; exit to DOS I tried opening command rather than cmd for my assembly code, but still I’ve waited for a long time and failed to assemble my code. I was told by Niel that it might be because of other issues, for the code assembled properly in his machine. What other issues it might be, I’ll still know about them. As for now, I was told that the output of my assembly code is ‘Yapinne’. I still need to modify this code, because I intend to display ‘Grainne Yap’. I’ll just keep you posted soon!
I’m using A86 as my assembler for my assembly codes. Click here to download assembler
1st lesson: Just a simple “Hello World” display MSG DB ‘Hello World$’
MOV AH, 09 ; print function
LEA DX, MSG ; string
INT 21H ; call dos
INT 20H ; return to dos
2nd lesson: Asking and displaying a number input
xxx:
MOV AH, 08 ; keyboard input w/ echo function
INT 21H ; call dos
CMP AL, 30H ; compare input with 0
JB xxx
CMP AL, 39H ; compare input with 9
JA xxx
MOV AH, 02 ; display a character function
MOV DL, AL ; copy character to DL, and display DL
INT 21H ; call dos
INT 20H ; exit dos
3rd lesson: More on conditions
msg db ‘Hello Guys!$’
here:
mov ah, 01 ; keyboard input with display function
int 21h ; call dos
cmp al, ‘x’ ; compare input with character ‘x’
je exit ; if input is ‘x’, go to exit
cmp al, ‘y’ ; compare input with character ‘y’
jne here ; if not equal to ‘y’, loop
mov ah, 09 ; if equal, display function
lea dx, msg ; display string ‘Hello Guys!’
int 21h ; call dos
exit:
int 20h ; exit dos