# Programming - Assembly Code Examples Hello, it's me again. Today I have 2 Code Examples for you. The first one will have text that describes what our programm should do and the second one will have a C-Like pseudocode that we will have to translate into assembly. So, let's get started!

# Code 1:

Write a programm in assembly that reads an positive integer value from the user (in decimal system) and prints it in binary system. Suppose we want only 8 numbers of binary, so we can only have decimal numbers from 0 to 255 as input. To do our calculations we will use divisions with 2, until the remainder is 0. The binary number is the remainders in opposite orders. We will store those remainders in an array and when finished we will print the binary number. The Console needs to print out 8 bits everytime. One way of doing it is setting each of the 8 array index values as 0. That way if our number is less then 8 bits long the rest bits will be 0, cause we initialized them like that.

The programm needs to print out something like that:

4

The binary number is:

00000100

## Code:

``.data``
``array: .space 32  # define Integer Array ``
``# define output strings``
``givedec: .asciiz "Please give the decimal number: "``
``binary: .asciiz "The binary number is: "``
``newline: .asciiz "\n"``

``.text``
``main:``

``# initialize array to 0``
``li \$t1,0``
``for0:``
``sw \$0,array(\$t1)``
``addi \$t1,\$t1,4``
``bge \$t1,32,endfor0``
``j for0``
``endfor0:``

``# input integer from user``
``#checking if it is between 0...255``
``li \$v0,4``
``la \$a0,givedec``
``syscall``
``#newline``
``li \$v0,4``
``la \$a0,newline``
``syscall``

``li \$t2,255 # max integer value``
``getint:``
``li \$v0,5``
``syscall``
``move \$s0,\$v0``
``bgt \$s0,\$t2,getint``

``# fill remainder array``
``li \$t0,0 # loop counter``

``for1:``
``# find remainder with 2``
``rem \$s1,\$s0,2 # s1=s0%2``

``# store in array``
``sw \$s1,array(\$t0)``

``# divide number with 2``
``div \$s0,\$s0,2 # s0=(int)s0/2``

``# increment loop counter``
``addi \$t0,\$t0,4``

``# check loop condition``
``bge \$t0,32,endfor1``
``j for1``
``endfor1:``

``# print binary representation``
``li \$v0,4``
``la \$a0,binary``
``syscall``
``#newline``
``li \$v0,4``
``la \$a0,newline``
``syscall``

``li \$t1,28 # loop counter``
``for2:``
``# load from array``
``lw \$s0,array(\$t1)``

``li \$v0,1``
``move \$a0,\$s0``
``syscall``

``# decrease loop counter``
``addi \$t1,\$t1,-4``

``# check loop condition``
``blt \$t1,\$0,endfor2``
``j for2``
``endfor2:``

``# terminate programm``
``li \$v0,10``
``syscall  ``

# Code 2:

Write an assembly programm that implements the insertion sort sorting algorithm. The array it will be applied on needs to be the following: int VECTOR.

The pseudocode looks like this:

``for(int i = 1; i < N; i++)``
``    for(int j = i; (j > 0) && (A[j]  < A[j-1]); j--)``
``        swap(A[j], A[j-1]);``

The programm prints out like that:

Give values for vector:

2

4

5

6

7

The vector after insertion sort is:

7

6

5

4

2

## Code:

``.data``
``Vector: .space 24 #int Vector``
``give: .asciiz "Give values for vector: "``
``aftersort: .asciiz "The vector after insertion sort is: "``
``newline: .asciiz "\n"``

``.text``
``main:``

``# print message for getting numbers``
``li \$v0,4``
``la \$a0,give``
``syscall``

``# newline``
``li \$v0,4``
``la \$a0,newline``
``syscall``

``# loop for getting numbers``
``li \$t0,0 # loop counter``
``for:``

``# get number``
``li \$v0,5``
``syscall``

``# store in array``
``move \$s0,\$v0``
``sw \$s0,Vector(\$t0)``

``# increment loop counter``
``addi \$t0,\$t0,4``

``# check loop condition``
``bge \$t0,24,endfor``
``j for``
``endfor:``

``# sort array using insertion sort``
``li \$t0,1 #i=1``
``fora: #int i=1,i<N;i++``

``addi \$t1,\$t0,0 #j=i``
``forb: #int j=i;j>0;j--``

``# swap code``
``mul \$t2,\$t1,4 #t2=j index``
``lw \$s0,Vector(\$t2) #s0=V[j]``
``addi \$t2,\$t2,-4 #t2= index of j-1``
``lw \$s1,Vector(\$t2) #s1=v[j-1]``

``# check condition``
``blt \$s0,\$s1,endforb #Vector[j]<Vector[j-1]``
``sw \$s0,Vector(\$t2) #A[j-1]=A[j]``
``addi \$t2,\$t2,4``
``sw \$s1,Vector(\$t2) #A[j]=A[j-1]``

``addi \$t1,\$t1,-1 #j--``
``ble \$t1,0,endforb #j<=0``
``j forb``
``endforb:``

``addi \$t0,\$t0,1 #i++``
``bge \$t0,6,endfora #i>=6``
``j fora``
``endfora:``

``# print array after sorting``
``li \$v0,4``
``la \$a0,aftersort``
``syscall``

``# newline``
``li \$v0,4``
``la \$a0,newline``
``syscall``

``li \$t0,0``
``for2:``
``bge \$t0,24,endfor2``
``lw \$s0,Vector(\$t0)``

``# print array value``
``li \$v0,1``
``move \$a0,\$s0``
``syscall``

``# newline``
``li \$v0,4``
``la \$a0,newline``
``syscall``

``addi \$t0,\$t0,4``
``j for2``
``endfor2:``

``# terminate programm``
``li \$v0,10``
``syscall  ``

This is the end of today's post. I had those 2 pretty good codes somewhere and saw that they were exactly were we are at our Tutorial Series and thought that it would be nice to upload them for you. I hope they helped you a little more with stuff that has to do with arrays and how we do stuff on them!

Next time we will get into functions and create an pseudo-dynamic array (we will talk about real dynamic ones also, some posts later on).

Bye! Until next time...

H2
H3
H4
3 columns
2 columns
1 column