Programming - Assembly Code Examples

in #programming7 years ago (edited)

    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:

Please give the decimal number:

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[6]. 

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[6]
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...

Sort:  

Could posting code be a way for programmers to make money with steem? Or are there more sophisticated ways to do this already?

Nice question.
The price of steem and sbd is actually high enough at the moment to earn some money even from posting pictures of your cat or dog in a daily basis. And it will reach even higher highs.

But if you write something useful for steemit like a tool, then you will earn even more, cause you are then actually being a helpful and appreciated member of the steemit community. For something like that you might need a lot of knowledge tho.

Remember this:
The better the content of a post, the more people it will attract. And by better I don't mean bigger, but explained in a scientific, right and "catchy" way.

So, you can post some code to earn money, but it might take a while to get the attention of the community. There is already a lot of programming there. This also means that the first posts might don't even earn cents.

I don't know if there is a better way right now.
Just do what you think is worth showing off, and the community will judge it!
Welcome to Steemit!
Steem on!
:)

Concerning more sophisticated ways, I should note the existence of utopian.io .

Congratulations @drifter1! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published a post every day of the week

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.17
TRX 0.14
JST 0.028
BTC 58097.21
ETH 2581.79
USDT 1.00
SBD 2.41