Monday, April 6, 2020

Lab 4 - part2: Adding Calculator


This is the second part of Lab4.
Our team chooses the option 1 as the second part.

Option 1: Adding Calculator

  1. Create a subroutine which enables the user to enter two numbers of up to two digits. Indicate where the cursor is, and allow the user to use the digit keys (0-9), backspace, and enter keys. Return the user's input value in the accumulator (A) register.
  2. Using this subroutine, write a program which add the two numbers (each of which is in the range 0-99) and print the result.
  • Optional challenge: extend to more than 2 digits and add a running total capability with a clear key.

1. Whole code
; ======== variables for screen input/output ========
define  SCINIT  $ff81 ; initialize/clear screen
define  CHRIN  $ffcf ; input character from keyboard
define  CHROUT  $ffd2 ; output character to screen
define  SCREEN  $ffed ; get screen size
define  PLOT    $fff0 ; get/set cursor coordinates

; ======== variables for key ========
define RIGHT $81
define LEFT $83
define ENTER $0d
define BACKSPACE $08

; ======== variables for Numbers that user input ========
define NUM1    $15;
define  NUM2    $16;

        jsr SCINIT

; ======= main Loop: print first num input question -> receive first Number
;                   -> print second num question -> receive second Number -> print result
;                   -> go back to the first step
mainLoop:
     ldy #$00
     jsr firstNumPrint ; first number input question
     jsr receiveNum; receive first number
     jsr firstNum_store  ; store the received number
     ldy #$00
     jsr secNumPrint  ; second number input question
     jsr receiveNum; receive second number
     jsr secNum_store  ; store the received number
     ldy #$00
     jsr resultPrint ; print a string 'Result'
     jsr result_print ; print the result
     jmp mainLoop ; go back to the first step


receiveNum:
     sec
     jsr PLOT
     ldx #$15
     clc
     jsr PLOT

receiveNum_loop:
     sec
     jsr PLOT
     jsr CHRIN


charCheck: 
     cmp #BACKSPACE ; if user enter backspace, it erase the #$15 digit
beq move_back

     cmp #RIGHT ; if user enter right arrow, it goes to the first digit
     beq move_right

     cmp #LEFT ; if user enter left arrow, it goes to the second digit
     beq move_left

     cmp #ENTER ; if user enter enter, it goes to the next process
     beq move

drawNum:
; ========= check whether user enters number (#$30-#$39) ==========
     cmp #$30
     bcc receiveNum_loop

     clc
     cmp #$3a
     bcs receiveNum_loop

     jsr CHROUT

     sec
     jsr PLOT
     cpx #$17
     bne receiveNum_loop
     dex
     clc
     jsr PLOT
     jmp receiveNum_loop

move_back:
cpx #$15
beq receiveNum_loop
jsr CHROUT
jmp receiveNum_loop

move_left: 
cpx #$15 ; first digit
     beq receiveNum_loop
     jsr CHROUT
     jmp receiveNum_loop

move_right: 
cpx #$16 ; second digit
     beq receiveNum_loop
     jsr CHROUT
     jmp receiveNum_loop

move:
     sec
     jsr PLOT
     ldx #$15 ; first degit
     clc
     jsr PLOT
     sec
     jsr PLOT

     clc
     sbc #$2F ; to calculate it, it should be subtracted by #$2f (hexadecimal for number 0: #$30)

     asl
     asl
    asl
     asl

     pha

     ldx #$16
     clc
     jsr PLOT
     sec
     jsr PLOT

     clc
     sbc #$2F ; to calculate it, it should be subtracted by #$2f (hexadecimal for number 0: #$30)
     pha

     ldx #$00
     iny
     clc
     jsr PLOT
     sec
     jsr PLOT

     pla
     tax
     pla

     rts

; ======== store the first number =========
firstNum_store:
     sta NUM1
     txa
     eor NUM1
     sta NUM1
     rts

; ======== store the second number =========
secNum_store:
     sta NUM2
     txa
     eor NUM2
     sta NUM2
     rts

; ========= print result ========
result_print:
     sec
     jsr PLOT
     ldx #$15
     clc
     jsr PLOT
     sec
     jsr PLOT

     sed
     lda NUM1
     adc NUM2
     cld
     pha

     bcc outputAddition
     ldx #$14
     clc
     jsr PLOT
     sec
     jsr PLOT
     lda #$31
     jsr CHROUT

; ========= calculate for result number =========
outputAddition:
     lsr
     lsr
     lsr
     lsr
     clc
     adc #$30 ; as the received number does not fit for ASCII, it needs to add #$30
     jsr CHROUT

     pla
     and #$0F
     clc
     adc #$30 ; as the received number does not fit for ASCII, it needs to add #$30
     jsr CHROUT

     sec
     jsr PLOT
     ldx #$00
     iny
     clc
     jsr PLOT

     rts

; ======== print 'ENTER FIRST NUMBER: 00 =======
firstNumPrint:
lda firstNum,y
        beq goback_main
        jsr CHROUT
        iny
        bne firstNumPrint

; ======== print "ENTER SECOND NUMBER: 00" ==========
secNumPrint:
lda secondNum,y
        beq goback_main
        jsr CHROUT
        iny
        bne secNumPrint

; ======== print "RESULT: " ==============
resultPrint:
lda result,y
        beq goback_main
        jsr CHROUT
        iny
        bne resultPrint

; ========= goes back to the main loop ========
goback_main:
     rts


firstNum:
dcb "E","N","T","E","R",32,"F","I","R","S","T",32,"N","U","M","B","E","R",":",32,32,"0","0"
dcb 00


secondNum:
dcb "E","N","T","E","R",32,"S","E","C","O","N","D",32,"N","U","M","B","E","R",":",32,"0","0"
dcb 00

result:
dcb "R","E","S","U","L","T",":"
dcb 00


This is our team's result. When we did this we had a lot of struggle time. I still did not exactly understand.

Lab5 - x86-64


This is the second part of Lab5.
We will print "Hello, World" 30 times with the order number using the x86-64.

x86-64 and aarch64 both use similar logic to use the loop but a little different kit.
Therefore, when we solve this part we use the same logic as part1 with different kits.
It is very interesting, I see how the machine language deal with "for loop" part in c.


1. Using the "objdump -d" command and find the <main>
"hello" for printf() version

"hello2" for write() version

"hello3" for syscall() wrapper version


2. Whole code
.text
.globl _start

_start:
movq $min, %r10 /*stores the min vale into %r10*/
movq $division, %r9 /*stores the division value 10 into %r9*/

loop:
cmp %r9, %r10 /*compare %r9 with %r10*/
jl digit_1 /* if the value is less or equal than 9(1-digit), go to the subroutine digit_1*/
jmp digit_2/* if the value is greater than 9(2-digit), go to the subroutine digit_2*/

digit_1:
movq %r10, %r15
add $'0', %r15 /*ascii number character*/
movq $msg+15, %r11 /* digit location within string*/
movb %r15b, (%r11) /*store the digit at the location */
jmp print /*go to print subroutine*/

digit_2:
movq $0, %rdx
movq %r10, %rax
div %r9
movq %rax, %r14
movq %rdx, %r15
add $'0', %r14/*ascii number character*/
add $'0', %r15/*ascii number character*/
movq $msg+14, %r11/* digit location within string*/
movb %r14b, (%r11) /*store the digit at the location */
movq $msg+15, %r12/* digit location within string*/
movb %r15b, (%r12) /*store the digit at the location */

jmp print/*go to print subroutine*/
print:
movq $len,%rdx /* message length */
movq $msg,%rsi /* message location */
movq $1,%rdi /* file descriptor stdout */
movq $1,%rax /* syscall sys_write */
syscall
inc %r12b
inc %r10
cmp $max, %r10
jne loop
movq $0,%rdi /* exit status */
movq $60,%rax /* syscall sys_exit */
syscall

.section .data
msg: .ascii "Hello, world!: 0\n"
len = . - msg
min = 0
max = 30
division = 10

3. Result




Tuesday, March 31, 2020

Project - stage1



Project – stage 1.

Spo600 final project is that I choose one open source to optimize.
For fist stage, choose open source package and build software. Benchmark the performance of the current implementation of the software on AArch64 and x86-64 systems. Lastly experiment with build options to determine if this has any impact on the performance.


1.     Choose open source

I choose “Zopfli (https://github.com/google/zopfli)” open source. It is compression algorithm made by Google.  Zopfli is written in C for portability. It is a compression-only library. Zopfli is bit-stream compatible with compression used in gzip, Zip, PNG, HTTP requests, and others.
If you compare compression algorithm, zopfli is slower than others. When compare the fast one, gzip 9, zopfli is slower more than 80times.

*https://www.lifehacker.com.au/2013/03/a-look-at-zopfli-googles-open-source-compression-algorithm/
 
Also, I saw “Zopfli Compression Algorithm is a compression library programmed in C to perform very good, but slow, deflate or zlib compression.” in zopfli GitHub.
So, I want to optimize this one.



    2.     Build the software
    2-1.  x86_64
    1)    clone the code to server
I cloned the data from the Zopfli GitHub.


2) add image to the server

For my testing, I will choose the 10mb png file  from https://www.sample-videos.com/download-sample-png-image.php.

3) Benchmark the performance
To benchmark the performance, I used 10mb, 20mb and 30mb files.
Real: elapsed real (wall clock) time used by the process, in seconds.
User: total number of CPU-seconds that the process used directly (in user mode), in seconds.
Sys: total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.

time
10mb
20mb
30mb
real
2m19.740s
3m38.955s
5m38.456s
user
1m22.996s
3m38.287s
5m37.188s
sys
0m0.163s
0m0.320s
0m0.728s

 * 10mb

* 20mb
* 30mb


I chosen 10mb file, executed 5times with O3 building option.
10mb
1st
2nd
3rd
4th
5th
Real
1m23.846s
1m23.742s1m23,551s1m24.355s4m58.047s
User
1m23.571s
1m23.469s1m23.282s1m24.060s1m33.316s
Sys
0m0.132s
0m0.133s0m0.132s0m0.150s0m0.154s


4) Experiment with build option.
I use the 10mb.png file with various build options.
-O0- no optimization
-O1- first level optimization
-O2 – second level optimization
-O3 – highest optimization
-Ofast – optimize for speed only
time
-O0
-O1
-O2
-O3
-Ofast
real
3m39.489s
1m41.897s1m33.908s2m19,740s1m23.712s
user
3m39.023s
1m41.606s1m33.620s1m22.996s
 1m23.455s
sys
0m0.142s
0m0.127s0m0.138s0m0.163s0m0.119s

 *-O0

*- O1


*-O2

*-O3

*-Ofast


2-2.  AArch64
1) Benchmark the performance
To benchmark the performance, I used 10mb, 20mb and 30mb files.
time
10mb
20mb
30mb
real
8m8.914s
18m56.510s
29m23.732s
user
8m7.776s
18m53.535s
29m18.217s
sys
0m0.229s
0m0.847s
0m2.001s

*10mb
* 20mb
*30mb

I chosen 10mb file, executed 5times with buildin option O3.
10mb
1st
2nd
3rd
4th
5th
Real
8m19.454s
8m42.835s8m31.470s
8m24.040s
8m8.914s
User
8m18.075s
8m41.506s8m30.148s
8m22.796s
8m7.776s
sys
0m0.339s
0m0.299s0m0.339s
0m0.319s
0m0.229s



2) Experiment with build option.
I use the 10mb.png file with various build options.
time
-O0
-O1
-O2
-O3
-Ofast
real
23m39.314s
10m5.855s
8m49.292s
8m8.914s
8m7.720s
user
23m36.622s
10m4.457s
8m48.034s
8m7.776s
8m6.445s
sys
0m0.330s
0m0.329s
0m0.310s
0m0.229s
0m0.349s

*-O0


*-O1


*-O2


*-O3


*-Ofast


When I change the building option the running time is also changed. no optimization(O0) is most slow and -Ofast is most fast. When the building option is changed except the code changing, the performance is changed. It is really interesting to me.
For stage2, I will profile the software to determine which part of the code is doing most of the work.