TU-Delft

Math pow in Assembly

I was programming some assembly the other day, and I noticed this specific example cannot be found anywhere! So I thought I’d post it :)

To run… save the file as power.s, then run

gcc -o power.o power.s -m32

(-m32 only if your on a 64bit system)

chmod +x power.o
./power.o

power.s

# ************************************************************************
# * Program name : pow                                                   *
# * Description  : Assembly Power function                               *
# * Author       : nicktc@gmail.com                                      *
# * Date         : 2011-05-26                                            *
# * License      : CC BY-NC-SA http://www.creativecommons.org            *
# ************************************************************************
.text

asknrstr:       .asciz "Please enter a natural number:"
asknrstr2:      .asciz "To the power of:"
printnrstr:     .asciz "%d^%d="
formatstr:      .asciz "%d" # Used in inout subroutine

.global main

# ************************************************************************
# * Subroutine  : main                                                   *
# * Description : application entry point                                *
# ************************************************************************
main:   movl    %esp, %ebp      # initialize the base pointer

ask:
        # Get number
        pushl   $asknrstr       # Push ask string
        call    printf          # Pop and print ask string
        subl    $4,%esp         # Reserve stack space for int
        leal    -4(%ebp), %eax  # Load address of stack space into %eax
        pushl   %eax            # Push argument of scanf
        pushl   $formatstr      # Push string
        call    scanf           # Scan number

        pushl   $asknrstr2      # Push ask string
        call    printf          # Pop and print ask string
        subl    $4,%esp         # Reserve stack space for int
        leal    -8(%ebp), %eax  # Load address of stack space into %eax
        pushl   %eax            # Push argument of scanf
        pushl   $formatstr      # Push string
        call    scanf           # Scan number

        pushl   -8(%ebp)        # Push number
        pushl   -4(%ebp)        # Push number
        pushl   $printnrstr     # Push print string
        call    printf          # Printf

        addl    $12,%esp        # Move stack pointer

        pushl   -8(%ebp)
        pushl   -4(%ebp)
        call    pow             # Call pow subroutine

        pushl   %eax            #
        pushl   $formatstr      # Push string format
        call    printf          # Print

end:    movl    $0,(%esp)       # push program exit code
        call    exit            # exit the program

# ************************************************************************
# * Subroutine  : pow                                                    *
# * Description : ask user for number and print sum                      *
# ************************************************************************
pow:
        pushl   %ebp            # Stack base pointer
        movl    %esp, %ebp      # Store stack pointer in %ebp

        movl    $1,%eax         # Store 1 in RESULT
        movl    8(%ebp),%ebx    # Store 'multiply by' in %ebx
        movl    12(%ebp),%ecx   # Store COUNT in %ecx

pow2:
        cmp     $0,%ecx
        jle     pow3

        subl    $1,%ecx         # Substract 1 from COUNT
        mul     %ebx            # Multiply %eax * %ecx (RESULT * NUMBER)

        cmp     $0,%ecx         # Compare COUNT to 0
        jg      pow2            # COUNT > 0 = jump to pow2
pow3:
        movl    %ebp, %esp      # Clear local variables from stack
        popl    %ebp            # Restore base pointer
        ret
Friday, June 10th, 2011 Programming, TU-Delft No Comments

TI-1200 Opdracht 8

I imagine a lot of people don’t know how to do this Java exercise, so here is some code :) Please do NOT copy it!!!

(I cannot redistribute the original assignment as this would be a breach of copyright. Please see blackboard here)

Adres.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.util.Scanner;

public class Adres {

	//post: creert een nieuw adres, met de opgegeven waarden.

	private String straat;
	private String huisNummer;
	private String postcode;
	private String plaats;

	/** Create a new adres
	 * @param straat
	 * @param huisNummer
	 * @param postcode
	 * @param plaats
	 */
	public Adres(String straat, String huisNummer, String postcode, String plaats)
	{
		this.straat = straat;
		this.huisNummer = huisNummer;
		this.postcode = postcode;
		this.plaats = plaats;
	}

	//post: retourneert een String-representatie van dit adres
	public String toString()
	{
		return "<Adres<straat=" + this.straat + ", huisNummer=" + this.huisNummer + ", postcode=" + this.postcode + ", plaats=" + this.plaats + ">>";
	}

	//post: retourneert true als other hetzelfde adres is ( zelfde postcode en huisnummer) als dit.
	public boolean equals(Object other)
	{
		if(other instanceof Adres)
		{
			Adres other2 = (Adres)other;

			if(other2.huisNummer == this.huisNummer
				&& other2.straat == this.straat
				&& other2.plaats == this.plaats
				&& other2.postcode == this.postcode
			)
				return true;
		}
		return false;
	}

	//
	/** Create an address from a scanner string
	 * pre: sc bevat een rij tokens die een adres voorstellen
	 * post: retourneert een Adres, opgebouwd uit de waarden die sc teruggeeft
	 * @param scanner text containing the address
	 */
	public static Adres read(Scanner scanner)
	{
		// scanner text ==
		// Emmalaan 23
		// 3051JC Rotterdam
		String straat = scanner.next();
		String huisNummer = scanner.next();
		String postcode = scanner.next();
		String plaats = scanner.next();

        return new Adres(straat, huisNummer, postcode, plaats);
	}

}

Adres_test.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Adres_test {

	public static void main(String [] args)
	{
		FileReader fileString;
		Adres test;

		System.out.println("test = Adres.read(sc);");
		try {
			fileString = new FileReader("src/Adres_test_text.txt");
			Scanner sc = new Scanner(fileString);

			 test = Adres.read(sc);

			System.out.println(test);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		System.out.println("---");

		Adres test1 = new Adres("Lorentzplein", "55", "2522EE", "Den Haag");
		Adres test2 = new Adres("Lorentzplein", "55", "2522EE", "Den Haag");

		System.out.println("test1:" + test1);
		System.out.println("test2:" + test2);
		System.out.println("test1.equals(test2):" + test1.equals(test2));

		System.out.println("---");

		test1 = new Adres("Buitenhof", "20", "2513AG", "Den Haag");
		System.out.println("test1:" + test1);
		System.out.println("test2:" + test2);
		System.out.println("test1.equals(test2):" + test1.equals(test2));
	}
}

Woning.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.util.Scanner;

public class Woning {
	private Adres adres;
	private int kamers;
	private int vraagPrijs;

	/** Create a Woning object
	 * post: creeert een Woning met de opgegeven waarden
	 *
	 * @param adres
	 * @param kamers
	 * @param vraagPrijs
	 */
	public Woning(Adres adres, int kamers, int vraagPrijs)
	{
		this.adres = adres;
		this.kamers = kamers;
		this.vraagPrijs = vraagPrijs;
	}

	//post: retourneert een String-representatie van deze woning
	public String toString()
	{
		return  "<Woning<adres=" + this.adres + ", kamers=" + this.kamers + ", vraagPrijs=" + this.vraagPrijs + ">>";
	}

	/** Check if the price is max
	 * post: retourneert true als vraagprijs = maxprijs
	 *
	 * @param maxprijs
	 * @return
	 */
	public boolean kostHooguit(int maxprijs)
	{
		return (this.vraagPrijs <= maxprijs);
	}

	//post: retourneert true als other dezelfde woning is (dwz hetzelfde adres) als deze.
	public boolean equals(Object other)
	{
		if(other instanceof Woning)
		{
			Woning otherWoning = (Woning) other;
			if(otherWoning.kamers == this.kamers
				&& otherWoning.vraagPrijs == this.vraagPrijs
				&& otherWoning.adres.equals(this.adres)
				)
				return true;

		}
		return false;
	}

	/** Read Woning from file
	 * pre: sc bevat een rij tokens die een woning voorstellen
	 * post: retourneert een Woning, opgebouwd uit de waarden die sc teruggeeft
	 *
	 * @param sc Scanner text input
	 * @return
	 */
	public static Woning read(Scanner sc)
	{
		/*
		 * Text ==
		 * Emmalaan 23
		 * 3051JC Rotterdam
		 * 7 kamers
		 * prijs 300000
		 */
		Adres adres = Adres.read(sc);
		int kamers = sc.nextInt();
		sc.next();
		sc.next();
		int price = sc.nextInt();

		return new Woning(adres,kamers,price);
	}

}

Woning_test_text.txt

Emmalaan 23
3051JC Rotterdam
7 kamers
prijs 300000

Woning_test.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Woning_test {

	public static void main(String [] args)
	{
		FileReader fileString;
		Woning test0;

		System.out.println("test0 = Woning.read(sc);");
		try {
			fileString = new FileReader("src/Woning_test_text.txt");
			Scanner sc = new Scanner(fileString);

			test0 = Woning.read(sc);

			System.out.println("test0:" + test0);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		System.out.println("---");

		Adres adres1 = new Adres("Lorentzplein", "55", "2522EE", "Den Haag");
		Adres adres2 = new Adres("Buitenhof", "20", "2513AG", "Den Haag");

		Woning test1 = new Woning(adres1,10,99);
		Woning test2 = new Woning(adres2,10,99);

		System.out.println("test1:" + test1);
		System.out.println("test2:" + test2);
		System.out.println("test1.equals(test2):" + test1.equals(test2));

		System.out.println("---");

		Woning test3 = new Woning(adres2,11,99);
		System.out.println("test3:" + test3);
		System.out.println("test2:" + test2);
		System.out.println("test3.equals(test2):" + test3.equals(test2));

		System.out.println("---");

		Woning test4 = new Woning(adres2,10,99);
		System.out.println("test4:" + test4);
		System.out.println("test2:" + test2);
		System.out.println("test4.equals(test2):" + test4.equals(test2));
	}
}

Portefeuille.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Portefeuille {

	private ArrayList<Woning> woningen;

	/** Initialize new Portefeuille
	 *
	 */
	public Portefeuille()
	{
		this.woningen = new ArrayList<Woning>();
	}

	/** Add a woning
	 * post: voegt de woning toe - tenminste als hij er nog niet in zat
	 * @param woning
	 */
	public void voegToe(Woning woning)
	{
		this.woningen.add(woning);
	}

	/** Get a list of woning'n with a max price
	 * post: retourneert een lijst met alle woningen die maxprijs of minder als vraagprijs	hebben
	 * @param maxprijs
	 * @return
	 */
	public ArrayList<Woning> woningenTot(int maxprijs)
	{
		Iterator<Woning> iter = this.woningen.iterator();
		ArrayList<Woning> result = new ArrayList<Woning>();

		Woning temp;

		while(iter.hasNext())
		{
			temp = iter.next();
			if(temp.kostHooguit(maxprijs))
				result.add(temp);
		}

		return result;
	}

	/** Get a Protefeuille from file
	 * pre : infile is de naam van een tekstbestand waarin op bovenbeschreven wijze een aantal woningen staan
	 * post: retourneert een Portefeuille, die de woningen, gelezen uit dit tekstbestand bevat
	 *
	 * @param infile filename
	 * @return
	 */
	public static Portefeuille read(String infile)
	{
		/*
		 * SAMPLE TEXT==
		 * 3
		 * Emmalaan 23
		 * 3051JC Rotterdam
		 * 7 kamers
		 * prijs 300000
		 * Javastraat 88
		 * 4078KB Eindhoven
		 * 3 kamers
		 * prijs 50000
		 * Javastraat 93
		 * 4078KB Eindhoven
		 * 4 kamers
		 * prijs 55000
		 */

		Portefeuille result = new Portefeuille();
		FileReader fileString;

		try {
			fileString = new FileReader(infile);
			Scanner sc = new Scanner(fileString);

			int amount = sc.nextInt();
			for(int i = 0; i < amount; i++)
				result.voegToe(Woning.read(sc));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return result;
	}

	public String toString()
	{
		String result;

		result = "<Portefeuille<woningen:";
		Iterator<Woning> iter = this.woningen.iterator();
		while(iter.hasNext())
		{
			result = result + iter.next() + ",";
		}
		result  = result + ">>";

		return result;
	}
}

Portefeuille_test_text.txt

3
Emmalaan 23
3051JC Rotterdam
7 kamers
prijs 300000
Javastraat 88
4078KB Eindhoven
3 kamers
prijs 50000
Javastraat 93
4078KB Eindhoven
4 kamers
prijs 55000

WoningDriver.java

//	Practicum TI1200,	Opdracht 8
//	Auteur N.Cate,		Studienummer 1342169
//	Datum 11-11-2010
//	NetID ntencate

import java.util.Iterator;
import java.util.Scanner;

public class Woningdriver {

	public static void main(String [] args)
	{
		System.out.println("Wat is de maximale prijs?");

		Scanner sc = new Scanner(System.in);
		int maxPrijs = sc.nextInt();

		Portefeuille port = Portefeuille.read("src/Portefeuille_test_text.txt");

		System.out.println("-----");

		Iterator<Woning> iter = port.woningenTot(maxPrijs).iterator();
		while(iter.hasNext())
			System.out.println(iter.next());
	}

}

Tags:

Friday, December 3rd, 2010 Hardware, TU-Delft No Comments