Assembly Language Exercise

C Code

#include <stdio.h>

void
hello(char *name, int hour, int min)
{

        printf("Hello, %s, it's %d:%02d.",
            name, hour, min);
}

int
main(void)
{

        hello(“Alan", 2, 55);
        return (0);
}

Instruction "Equivalences"

pushq %r

--similar to:--
subq $8, %rsp
movq %r, (%rsp)

---------------

call <label>

--similar to:--
subq $8, %rsp
movq PC, (%rsp)
mov <label>, PC

---------------

leave

--similar to:--
movq %rbp, %rsp
popq %rbp

Assembly Code

        .file   "main.c"
        .section        .rodata
.LC0:
        .string "Hello, %s, it's %d:%02d."
        .text
.globl hello
        .type   hello, @function
hello:
.LFB2:
        pushq   %rbp
.LCFI0:
        movq    %rsp, %rbp
.LCFI1:
        subq    $16, %rsp
.LCFI2:
        movq    %rdi, -8(%rbp)
        movl    %esi, -12(%rbp)
        movl    %edx, -16(%rbp)
        movl    -16(%rbp), %ecx
        movl    -12(%rbp), %edx
        movq    -8(%rbp), %rsi
        movl    $.LC0, %edi
        movl    $0, %eax
        call    printf
        leave
        ret
.LFE2:
        .size   hello, .-hello
        .section        .rodata
.LC1:
        .string "Alan"
        .text
.globl main
        .type   main, @function
main:
.LFB3:
        pushq   %rbp
.LCFI3:
        movq    %rsp, %rbp
.LCFI4:
        movl    $55, %edx
        movl    $2, %esi
        movl    $.LC1, %edi
        call    hello
        movl    $0, %eax
        leave
        ret
.LFE3:
        .size   main, .-main

Machine State

For this exercise, you should update the machine state, starting with the initial values given below, as the program executes starting at the beginning of main going up to, but not including, the call to printf.

Memory

0x700000000088
0x700000000084
0x700000000080
0x70000000007c
0x700000000078
0x700000000074
0x700000000070
0x70000000006c
0x700000000068
0x700000000064
0x700000000060
0x70000000005c
0x700000000058
0x700000000054
0x700000000050
0x70000000004c
0x700000000048
0x700000000044
0x700000000040
0x70000000003c
0x700000000038
0x700000000034
0x700000000030
0x70000000002c
0x700000000028
0x700000000024
0x700000000020

Initial Values

%rbp
0x700000000088
%rsp
0x70000000006c
.LC0
0x408280
.LC1
0x408400
&"movl $0, %eax" in main()
0x400220

Registers

%rbp
%rsp
%rdi
%rsi
%rax
%rcx
%rdx