Monday, April 6, 2020

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




No comments:

Post a Comment