# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

.function u1 main(){
    movi v0, 45000
    movi v1, 256
    movi v2, 46080000
    call test, v0, v1, v2, v0
    return
}

.function i32 test(i32 a0, i32 a1, i32 a2){
    movi v3, 0   #sum
    movi v4, 0   #loop counter
    ldai 0
loop:
    movi v5, 0   #loop2_counter
    jeq a0, loop_exit
    lda v5
loop2:
    jeq a1, loop2_exit
    call.short bitsinbyte, v5, v0
    add2 v3
    sta v3
    inci v5, 1
    lda v5
    jmp loop2
loop2_exit:
    inci v4, 1
    lda v4
    jmp loop
loop_exit:
    lda v3
    jne a2, assert_err
    ldai 0
    return
assert_err:
    ldai 1
    return
}

.function i32 bitsinbyte(i32 a0){
    movi v1, 1       #m
    movi v2, 0       #c
loop:
    ldai 0x100
    jle v1, loop_exit
    and a0, v1
    jnez if_b
endif:
    lda v1
    shli 1
    sta v1
    jmp loop
loop_exit:
    lda v2
    return
if_b:
    inci v2, 1
    jmp endif
}