20 สิงหาคม 2554

[Assembly]โปรแกรมแปลงค่าในตาราง


.model small
.data
TAB1  DB ‘0123456789’
VBCD DB 05h
VASC  DB 30h
.code
mov ax,@data
mov ds,ax

mov ax,0000h
mov al,VBCD
mov di,ax
mov al,TAB1[di]

mov VASC,al

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรมแปลงค่าในตาราง โดยใช้การคูณ


.model small
.data
    TBCD    DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
   DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
   DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
   DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
   DB 'Even',13,10,'$'
   DB 'Odd ',13,10,'$'
STR1    DB 'ENTER NUMBER: $'
STR2    DB 13,10,'Number is $'
SCALE   DB 7
VDEC    DB '0'
VBCD    DB 00h
.code
mov ax,@data
mov ds,ax

    mov dx,offset STR1
mov ah,09h
int 21h

mov ah,01h
int 21h

mov VDEC,al
sub al,'0'
mov VBCD,al
mov dx,offset STR2
mov ah,09h
int 21h

mov dx,offset TBCD
mov al,VBCD
mul SCALE
add dx,ax
mov ah,09h
int 21h

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรม บวก ลบ แบบ BCD (ป้อนค่าและแสดงผลได้)


.model small
.data
BALANCE    DB 'Balance: $'
RECEIVE    DB 13,10,'Receive: $'
SELL    DB 13,10,'Sell: $'
REMAIN    DB 13,10,'Remain = '
VRemain      DB '00000',13,10,'$'
VBalance      DB 5,?,'0000'
VReceive      DB 5,?,'0000'
VSell      DB 5,?,'0000'
.code
mov ax,@data
mov ds,ax

mov dx,offset BALANCE       ;input Balance    
mov ah,09h
int 21h
mov dx,offset VBalance
mov ah,0ah
int 21h

mov dx,offset RECEIVE       ;input Receive
mov ah,09h
int 21h
mov dx,offset VReceive
mov ah,0ah
int 21h

mov dx,offset SELL          ;input Sell
mov ah,09h
int 21h
mov dx,offset VSell
mov ah,0ah
int 21h
 
    mov al,VBalance+5           ;digit 4
add al,VReceive+5
aaa
add al,30h
sbb al,VSell+5
aas
mov bl,al

mov al,VBalance+4           ;digit 3
adc al,VReceive+4
aaa
add al,30h
sbb al,VSell+4
aas
mov bh,al

mov al,VBalance+3           ;digit 2
adc al,VReceive+3
aaa
add al,30h
sbb al,VSell+3
aas
mov dl,al

mov al,VBalance+2           ;digit 1
adc al,VReceive+2
aaa
mov dh,al

mov al,30h                  ;digit 0
adc al,VRemain
aaa
add al,30h
mov VRemain,al

mov al,dh                   ;digit 1
add al,30h
sbb al,VSell+2
aas
mov dh,al

mov al,VRemain              ;digit 0
sbb al,0
aas
add al,30h
mov VRemain,al

add bx,3030h                ;keep digit 1-4
mov VRemain+3,bh
mov VRemain+4,bl
add dx,3030h
mov VRemain+1,dh
mov VRemain+2,dl

mov dx,offset REMAIN        ;show digit 0-4
mov ah,09h
int 21h

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรมที่ใช้คำสั่งเงื่อนไขและวนรอบ


.model small
.data
STR1 DB 'ENTER 1-9: $'
STR2 DB 13,10,'SUM: '
    VSUM DB '00',13,10,'$'
    VN   DB 13,10,'INVAID INPUT','$'
    NASC DB '0'
    KEY  DB '0'
.code
mov ax,@data
mov ds,ax

  mov dx,offset STR1
mov ah,09h
int 21h
 
  mov ah,01h
    int 21h
    mov KEY,al
    mov bx,ax
 
CHK:mov al,KEY
    cmp al,'0'
    jb NON
    cmp al,'9'
    ja NON
    mov ax,bx
    jmp L0
 
NON:mov dx,offset VN
    jmp EX
 
L0: mov NASC,al
    mov ax,3030h
mov cl,'1'

L1: add al,cl
    aaa
    add al,'0'
    inc cl
    cmp cl,NASC
jna L1

    mov VSUM,ah
    mov VSUM+1,al
 
    mov dx,offset STR2
EX: mov ah,09h
int 21h

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรม บวก ลบ แบบ BCD


.model small
.data
    SUM    dw 0
    START  dw 1234H
    VAdd   dw 3099H
    VSub   dw 2099H
.code  
    MOV ax,@data
    MOV ds,ax
 
;-------------------- Addition
 
MOV DX,START     ;load START
MOV BX,VADD        ;load ValueAddition
    MOV AL,BL       ;sum BL with DL
    ADD AL,DL
    DAA             ;adjust
    MOV CL,AL       ;answer to CL
    MOV AL,BH       ;sum BH, DH, and carry
    ADC AL,DH
    DAA             ;adjust
    MOV CH,AL       ;answer to CH

;-------------------- Subtraction

    MOV DX,Vsub     ;load ValueAddition
    MOV BX,CX        ;load Answer of Addition
MOV AL,BL       ;subtract DL from BL
    SUB AL,DL
DAS             ;adjust
MOV CL,AL       ;answer to CL
MOV AL,BH       ;subtract DH, and carry
SBB AL,DH
DAS             ;adjust
    MOV CH,AL       ;answer to CH

    MOV ax,4c00h
    INT 21h
end

[JAVA]การใช้ตัวแปรอะเรย์ของข้อมูลชนิดคลาส แบบฝึกหัดที่ 6.2


------------------------------------
              Account
------------------------------------

package Banking;

public class Account {
    protected double balance;
    public Account(double amount){
        balance = amount;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            result = false;
        }
        return result;
    }
    public double getBalance(){
        return balance;
    }
    public void showBalance(){
        System.out.println(balance);
    }
    public Account(){
     
    }
}

------------------------------------
              Customer
------------------------------------

package Banking;

public class Customer {
    private String firstName;
    private String lastName;
    private Account acct[];
    private int NumAccount=0;
    public Customer(String fName,String lName){
        firstName = fName;
        lastName = lName;
        acct = new Account[20];
    }
    public void setFirstName(String fName){
        firstName = fName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setLastName(String lName){
        lastName = lName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setAccount(Account acct){
        acct = acct;
    }
    public Account getAccount(int index){
        return acct[index];
    }
    public void addAccount(Account acct){
        this.acct[NumAccount]=acct;
        NumAccount++;
    }
    public int getNumAccount(){
        return NumAccount;
    }
    public void printReport()
    {
        for(int i=0;i<getNumAccount();i++)
        System.out.println("Account"+(i+1)+" Balance :"+getAccount(i).getBalance());
    }
}

------------------------------------
               Teller
------------------------------------

import Banking.*;

public class Teller {
    public static void main(String args[]){
     
        Customer cust = new Customer("Somchai","Sommut");
        Account acct1 = new Account(5000);
        Account acct2 = new Account(3000);
     
        cust.addAccount(acct1);
        cust.addAccount(acct2);
 
        acct1.deposit(4200);
        acct1.withdraw(3000);
     
        acct2.deposit(3200);
        acct2.withdraw(1000);
     
        System.out.println(cust.getFirstName()+"  "+cust.getLastName());
        System.out.println("Number of Account : "+cust.getNumAccount());
     
        cust.printReport();
    }  
}

15 สิงหาคม 2554

[JAVA]โปรแกรมจำลองระบบธนาคาร แบบฝึกหัดที่ 5.1


---------- Account Class ----------

package Banking;

public class Account {
    private double balance;
    public Account(double amount){
        balance = amount;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public void withdraw(double amount){
            balance = balance - amount;
    }
    public double getBalance(){
        return balance;
    }
    public void showBalance(){
        System.out.println(balance);
    }
    public Account(){
     
    }
}


---------- Customer Class ----------

package Banking;

public class Customer {
    private String firstName;
    private String lastName;
    private Account acct;

    public Customer(String fName,String lName){
        firstName = fName;
        lastName = lName;
    }
    public void setFirstName(String fName){
        firstName = fName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setLastName(String lName){
        lastName = lName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setAccount(Account acct){
        acct = acct;
    }
    public Account getAccount(){
        return acct;
    }
}

---------- Teller Class ----------

import Banking.*;

public class Teller {
    public static void main(String args[]){
        boolean result;
        Account myAccount = new Account(4000);
        Customer cust = new Customer("Somchai","Sommut");
        cust.setAccount(myAccount);
        result = myAccount.withdraw(3000);
        myAccount.deposit(4200);
        myAccount.showBalance();
        System.out.println("Withdraw = "+result);
        System.out.println(cust.getFirstName()+" "+cust.getLastName());
    }  
}

[JAVA]การเขียน Constructor และเมธอดแบบ Overload แบบฝึกหัดที่ 5.4


------------------------------------
                Main
------------------------------------

public class TestRectangle {
    public static void main(String args[]){
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle(10.5,8,"Red");
        rect1.setWidth(12.0);
        rect1.setHeight(5.5);
        rect1.setColor("Yellow");
        System.out.println("Rect1 : Width = "+rect1.getWidth()+", Height = "+rect1.getHeight()+", Color = "+rect1.getColor());
        System.out.println("Rect2 : Whith = "+rect2.getWidth()+", Height = "+rect2.getHeight()+", Color = "+rect2.getColor());  
        System.out.println("Area1 : "+rect1.findArea()+"\n"+"Area2 : "+rect2.findArea());
    }
}

------------------------------------
             Rectangle
------------------------------------

public class Rectangle {
    private double width =1;
    private double height = 1;
    private static String color = "white";
    public Rectangle(){
     
    }
    public Rectangle(double w,double h,String c){
        width=w;
        height=h;
        color=c;
    }
    public void setWidth(Double w){
        width=w;
    }
    public double getWidth(){
        return width;
    }
    public void setHeight(double h){
        height=h;
    }
    public double getHeight(){
        return height;
    }
    public void setColor(String c){
        color=c;
    }
    public String getColor(){
        return color;
    }
    public double findArea(){
        return width*height;
    }
}

[JAVA]การเขียนโปรแกรมที่ใช้หลักการเชิงออปเจ็ค แบบฝึกหัดที่ 5.3


------------------------------------
             Account
------------------------------------

package Banking;

public class Account {
    protected double balance;
    public Account(double amount){
        balance = amount;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            result = false;
        }
        return result;
    }
    public double getBalance(){
        return balance;
    }
    public void showBalance(){
        System.out.println(balance);
    }
    public Account(){
     
    }
}

------------------------------------
             Customer
------------------------------------

package Banking;

public class Customer {
    private String firstName;
    private String lastName;
    private Account acct[];
    private int AccountNum=0;
    public Customer(String fName,String lName){
        firstName = fName;
        lastName = lName;
        acct = new Account[20];
    }
    public void setFirstName(String fName){
        firstName = fName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setLastName(String lName){
        lastName = lName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setAccount(Account acct){
        acct = acct;
    }
    public Account getAccount(int index){
        return acct[index];
    }
    public void addAccount(Account acct){
        this.acct[AccountNum]=acct;
        AccountNum++;
    }
    public int getAccountNum(){
        return AccountNum;
    }
    public void printReport()
    {
        for(int i=0;i<getAccountNum();i++)
        System.out.println("Account"+(i+1)+" Balance :"+getAccount(i).getBalance());
    }
}

------------------------------------
           SavingAccount
------------------------------------

package Banking;

public class SavingAccount extends Account {

    public SavingAccount(double amount) {
        balance = amount;
    }
}

------------------------------------
            CheckingAccount
------------------------------------

package Banking;

public class CheckingAccount extends Account {
    private double credit;
    public CheckingAccount(double initBalance,double credit){
        balance = initBalance;
        credit=credit;
    }
    public double getCredit(){
        return credit;
    }
    @Override
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            if(amount > (balance + credit)){
                result = false;
            }
            else{
                balance = 0;
                credit = (balance + credit) - amount;
            }    
        }
        return result;
    }
}

------------------------------------
              Tailer
------------------------------------

import Banking.*;

public class Teller {
    public static void main(String args[]){
     
        Customer cust = new Customer("Somchai","Sommut");
        SavingAccount acct1 = new SavingAccount(3000);
        CheckingAccount acct2 = new CheckingAccount(4000,2000);
     
        cust.addAccount(acct1);
        cust.addAccount(acct2);
     
        acct1.deposit(4200);
        acct1.withdraw(3000);
     
        acct2.deposit(3200);
        acct2.withdraw(1000);
     
        System.out.println(cust.getFirstName()+"  "+cust.getLastName());
        System.out.println("Number of Account : "+cust.getAccountNum());
     
        cust.printReport();
    }  
}

[JAVA]การเขียนเมธอด withdraw() แบบฝึกหัดที่ 5.2

---------- Account Class ----------

package Banking;

public class Account {
    private double balance;
    public Account(double amount){
        balance = amount;
    }
    public void deposit(double amount){
        balance = balance + amount;
    }
    public boolean withdraw(double amount){
        boolean result = true;
        if(amount < balance){
            balance = balance - amount;
        }
        else{
            result = false;
        }
        return result;
    }
    public double getBalance(){
        return balance;
    }
    public void showBalance(){
        System.out.println(balance);
    }
    public Account(){
     
    }
}


---------- Customer Class ----------

package Banking;

public class Customer {
    private String firstName;
    private String lastName;
    private Account acct;

    public Customer(String fName,String lName){
        firstName = fName;
        lastName = lName;
    }
    public void setFirstName(String fName){
        firstName = fName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setLastName(String lName){
        lastName = lName;
    }
    public String getLastName(){
        return lastName;
    }
    public void setAccount(Account acct){
        acct = acct;
    }
    public Account getAccount(){
        return acct;
    }
}

---------- Teller Class ----------

import Banking.*;

public class Teller {
    public static void main(String args[]){
        boolean result;
        Account myAccount = new Account(4000);
        Customer cust = new Customer("Somchai","Sommut");
        cust.setAccount(myAccount);
        result = myAccount.withdraw(3000);
        myAccount.deposit(4200);
        myAccount.showBalance();
        System.out.println("Withdraw = "+result);
        System.out.println(cust.getFirstName()+" "+cust.getLastName());
    }  
}

05 สิงหาคม 2554

[Assembly]โปรแกรม บวก ลบ โดยกำหนดค่าของตัวแปรอยู่ในรูปของเลขรหัส BCD (4หลัก)

.model small
.data
    SUM    dw 0
    START  dw 1234H
    VAdd   dw 3099H
    VSub   dw 2099H
.code  
    MOV ax,@data
    MOV ds,ax
 
;-------------------- Addition
 
MOV DX,START     ;load START
MOV BX,VADD        ;load ValueAddition
    MOV AL,BL       ;sum BL with DL
    ADD AL,DL
    DAA             ;adjust
    MOV CL,AL       ;answer to CL
    MOV AL,BH       ;sum BH, DH, and carry
    ADC AL,DH
    DAA             ;adjust
    MOV CH,AL       ;answer to CH

;-------------------- Subtraction

    MOV DX,Vsub     ;load ValueAddition
    MOV BX,CX        ;load Answer of Addition
MOV AL,BL       ;subtract DL from BL
    SUB AL,DL
DAS             ;adjust
MOV CL,AL       ;answer to CL
  MOV AL,BH       ;subtract DH, and carry
SBB AL,DH
DAS             ;adjust
    MOV CH,AL       ;answer to CH

    MOV ax,4c00h
    INT 21h
end

[Assembly]โปรแกรม บวก ลบ คูณ หาร แบบคิดตัวทด

.model small
.data
CSUM   DW 0
CREN   DW 0
COM    DB 0
CADD   DB 24
CSUB   DB 10
CMUL   DB 3
CDIV   DB 4
.code
mov ax,@data
mov ds,ax

mov ax,CSUM
mov al,COM
add al,CADD
adc ah,0
     
sub al,CSUB
sbb ah,0
 
 mov bl,CMUL
mov bh,0
mul bx
 
 mov bl,CDIV
div bx
mov CSUM,ax
mov ax,0
div bx
mov CREN,ax

mov ax,4c00h
int 21h
end

[Assembly]โปรแกรม บวก ลบ คูณ หาร แบบปัดเศษทศนิยม

.model small
.data
CSUM   DW 0
COM    DB 0
CADD   DB 24
CSUB   DB 8
CMUL   DB 4
CDIV   DB 3
.code
mov ax,@data
mov ds,ax

mov ax,CSUM
mov al,COM
add al,CADD
adc ah,0
     
sub al,CSUB
sbb ah,0
 
 mov bl,CMUL
mov bh,0
mul bx
 
 mov bl,CDIV
div bx
add ah,ah
cmp ah,bl
jb NEXT
inc al

NEXT:  mov CSUM,ax

 mov ax,4c00h
int 21h
end